DoublePredicate Interface is a part of the java.util.function package which is introduced in Java 8. It is an in-built Functional Interface. This function expect one double-valued argument as an input and produces a boolean value as an output. As it is a functional interface, it can be used assignment target for lambda expression or method reference. In this post, we are going to see several implementations of DoublePredicate Interface by using different examples.

DoublePredicate_Interface_Java8_Techndeck

DoublePredicate Interface in Java 8 with Examples...!!! Share on X

Look at DoublePredicate Javadoc description below:

DoublePredicateInterface_Signature_Java8_Techndeck

DoublePredicate Interface contains 4 methods:

  1. test

  2. and

  3. or

  4. negate

Let’s discuss these methods:

test 

This method evaluates predicate on the given argument and produces a boolean result.

boolean test(double value);

and 

This method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. When evaluating the composed predicate, if this predicate is false, then the other predicate is not evaluated.

default DoublePredicate and(DoublePredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) && other.test(value);
    }

or 

This method returns a composed predicate that represents a short-circuiting logical OR of this predicate and another. When evaluating the composed predicate, if this predicate is true}, then the other predicate is not evaluated.

default DoublePredicate or(DoublePredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) || other.test(value);
    }

negate 

This method returns a predicate that represents the logical negation of this predicate.

default DoublePredicate negate() {
        return (value) -> !test(value);
    }

Lets understand above mentioned methods thorough various examples:

Example 1. ‘test’ method

import java.util.function.DoublePredicate;

public class DoublePredicateInterfaceJava8Example1 {

    public static void main(String[] args) {

        System.out.println("DoublePredicate Interface - Example 1");

        DoublePredicate doublePredicateObj = i -> i == -10;

        System.out.println("Are numbers matching: " + doublePredicateObj.test(10));

        System.out.println("Are numbers matching: " + doublePredicateObj.test(-10));
    }

}

Example 2. ‘and’, ‘or’ and ‘negate’ methods

import java.util.function.DoublePredicate;

public class DoublePredicateInterfaceJava8Example2 {

    public static void main(String[] args) {

        System.out.println("DoublePredicate Interface - Example 2");

        DoublePredicate doublePredicateObj1 = i -> i >= 10;

        DoublePredicate doublePredicateObj2 = i -> i == 5;

        DoublePredicate doublePredicateObj3 = i -> i < 70;

        //and operation
        System.out.println("Result 1: " + doublePredicateObj1.and(doublePredicateObj2).test(15));

        //or operation
        System.out.println("Result 2: " + doublePredicateObj1.or(doublePredicateObj2).or(doublePredicateObj3).test(15));

        //negate operation
        System.out.println("Result 3: " + doublePredicateObj3.negate().test(10));
    }

}

Java 8 DoublePredicate Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it.

DoublePredicate Interface in Java 8 with Examples...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts