What is the DoublePredicate functional interface in Java?

DoublePredicate is a functional interface, which accepts a double valued argument and returns a boolean value. This interface has the following methods:

  1. test(double value)
  2. and(DoublePredicate other)
  3. negate()
  4. or(DoublePredicate other)

The DoublePredicate interface is defined in the java.util.function package. To import the DoublePredicate interface, we will check the following import statement:

import java.util.function.DoublePredicate;

The test(double value) method

The test(double value) method is used to test a given double valued input on certain evaluations.

Syntax


boolean test(double value)

Parameters

  • double value: This method accepts a value of type double.

Return value

This method returns the result of the evaluation, that is, either True or False.

Code

import java.util.function.DoublePredicate;;
public class Main{
public static void main(String[] args) {
// DoublePredicate to check if the double valued argument is even
DoublePredicate isEven = arg -> arg % 2 == 0;
// test whether 4 is even
System.out.println(isEven.test(4));
// test whether 3 is even
System.out.println(isEven.test(3));
}
}

The and(DoublePredicate other) method

The and(DoublePredicate other) method is used to chain multiple implementations of the DoublePredicate together as a short-circuiting and logical AND operation. If any implementation returns False during the evaluation, then the subsequent implementations are not invoked.

Syntax


default DoublePredicate and(DoublePredicate other)

Parameters

  • DoublePredicate other: This is a DoublePredicate that needs to be evaluated.

Return value

This method returns a composed predicate.

Code

import java.util.function.DoublePredicate;;
public class Main{
public static void main(String[] args) {
// DoublePredicate to check if the double valued argument is even
DoublePredicate isEven = arg -> arg % 2 == 0;
// DoublePredicate to check if the double valued argument is less than 100
DoublePredicate isLessThanHundred = arg -> arg < 100;
// Composed DoublePredicate
DoublePredicate composedPredicate = isEven.and(isLessThanHundred);
// test whether 4 is even and less than 100
System.out.println(composedPredicate.test(4));
// test whether 103 is even and less than 100
System.out.println(composedPredicate.test(103));
}
}

The negate() method

The negate() method is used to return a DoublePredicate, which is a negation of the given DoublePredicate.

Syntax


default DoublePredicate negate()

Parameters

This method has no parameters.

Return value

This method returns a predicate.

Code

import java.util.function.DoublePredicate;;
public class Main{
public static void main(String[] args) {
// DoublePredicate to check if the double valued argument is even
DoublePredicate isEven = arg -> arg % 2 == 0;
// DoublePredicate to get the Opposite of even using the negate method
DoublePredicate isEvenNegate = isEven.negate();
// test negation on 4
System.out.println(isEvenNegate.test(4));
// test negation on 3
System.out.println(isEvenNegate.test(3));
}
}

The or(DoublePredicate other) method

The or(DoublePredicate other) method is used to chain multiple implementations of the DoublePredicate together as a short-circuiting and logical OR operation. If any implementation returns True during the evaluation, then the subsequent implementations are not invoked.

Syntax


default DoublePredicate or(DoublePredicate other)

Parameters

  • DoublePredicate other: This is a DoublePredicate that needs to be evaluated.

Return value

This method returns a composed predicate.

Code

import java.util.function.DoublePredicate;;
public class Main{
public static void main(String[] args) {
// DoublePredicate to check if the double valued argument is even
DoublePredicate isEven = arg -> arg % 2 == 0;
// DoublePredicate to check if the double valued argument is less than 100
DoublePredicate isLessThanHundred = arg -> arg < 100;
// Composed DoublePredicate
DoublePredicate composedPredicate = isEven.or(isLessThanHundred);
// test whether 4 is even and less than 100
System.out.println(composedPredicate.test(4));
// test whether 103 is even and less than 100
System.out.println(composedPredicate.test(103));
}
}

Free Resources