The DoublePredicate interface is a specialized functional interface introduced in Java 8 and available in the java.util.function package. It represents a predicate (boolean-valued function) that accepts a single primitive double value as input and returns either true or false based on a specified condition
- Primitive specialization of Predicate<Double>.
- Avoids boxing and unboxing of Double objects.
- Supports lambda expressions and method references.
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
// Check if a number is greater than 50
DoublePredicate check =
num -> num > 50;
System.out.println(check.test(75.5));
}
}
Output
true
Explanation: In this example, a DoublePredicate is created using a lambda expression that checks whether a number is greater than 50. The test() method evaluates the condition for 75.5 and returns true because the value is greater than 50.
Syntax:
@FunctionalInterface
public interface DoublePredicate
Methods of DoublePredicate Interface
1. test() Method
The test() method is the functional method of the DoublePredicate interface. It evaluates a condition on the given double value and returns true if the condition is satisfied; otherwise, it returns false.
- Core method of the interface.
- Used to evaluate conditions.
Syntax:
boolean test(double value)
Parameters: value- The input double value.
Return Value: Returns true if the condition is satisfied; otherwise false.
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
DoublePredicate isPositive =
x -> x > 0;
System.out.println(isPositive.test(10.5));
}
}
Output
true
Explanation: In this example, the predicate checks whether a number is positive. Since 10.5 is greater than 0, the test() method returns true.
2. and() Method
The and() method combines the current predicate with another predicate using the logical AND (&&) operation. The resulting predicate returns true only if both predicates return true.
- Returns a new DoublePredicate.
- Supports predicate chaining.
Syntax:
default DoublePredicate and(DoublePredicate other)
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
DoublePredicate greaterThan10 = n -> n > 10;
DoublePredicate lessThan100 = n -> n < 100;
System.out.println(
greaterThan10.and(lessThan100).test(50)
);
}
}
Output
true
Explanation: Returns true because 50 is greater than 10 and less than 100.
3. or() Method
The or() method combines two predicates using the logical OR (||) operation. The resulting predicate returns true if at least one predicate evaluates to true.
- Supports predicate composition.
- Uses short-circuit evaluation.
Syntax:
default DoublePredicate or(DoublePredicate other)
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
DoublePredicate lessThan0 = n -> n < 0;
DoublePredicate greaterThan100 = n -> n > 100;
System.out.println(
lessThan0.or(greaterThan100).test(150)
);
}
}
Output
true
Explanation: Returns true because 150 satisfies the second condition.
4. negate() Method
The negate() method returns a predicate that represents the logical negation of the current predicate. It reverses the result of the original condition.
- Returns a new predicate.
- Useful for opposite conditions.
Syntax:
default DoublePredicate negate()
import java.util.function.DoublePredicate;
public class Main {
public static void main(String[] args) {
DoublePredicate isPositive = n -> n > 0;
System.out.println(
isPositive.negate().test(-5)
);
}
}
Explanation: negate() reverses the condition, so it returns true for a negative number.
DoublePredicate vs Predicate<Double>
| Feature | DoublePredicate | Predicate |
|---|---|---|
| Input Type | double | Double |
| Return Type | boolean | boolean |
| Boxing/Unboxing | Not Required | Required |
| Performance | Faster | Slightly Slower |
| Package | java.util.function | java.util.function |
Example: Java example to demonstrate DoublePredicate interface
import java.util.function.DoublePredicate;
public class DoublePredicateDemo {
public static void main(String[] args)
{
// DoublePredicate to check square
// of x is less than 100
DoublePredicate db
= (x) -> { return x * x < 100.0; };
System.out.println("100 is less than 100 "
+ db.test(10));
DoublePredicate db3;
// Test condition reversed
db.negate();
System.out.println("100 is greater than 100 "
+ db.test(10));
DoublePredicate db2 = (x) ->
{
double y = x * x;
return y >= 36 && y < 1000;
};
// Test condition ANDed
// with another predicate
db3 = db.and(db2);
System.out.println("81 is less than 100 "
+ db3.test(9));
db3 = db.or(db2);
// Test condition ORed with another predicate
System.out.println("49 is greater than 36"
+ " and less than 100 "
+ db3.test(7));
}
}
Output
100 is less than 100 false 100 is greater than 100 false 81 is less than 100 true 49 is greater than 36 and less than 100 true