The IntPredicate 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 int value as input and returns either true or false based on a specified condition.
- Primitive specialization of Predicate<Integer>.
- Avoids boxing and unboxing of Integer objects.
- Supports lambda expressions and method references.
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
// Check if a number is even
IntPredicate isEven = num -> num % 2 == 0;
System.out.println(isEven.test(20));
}
}
Output
true
Explanation: In this example, an IntPredicate checks whether a number is even. The test() method evaluates the condition for 20 and returns true.
Syntax
@FunctionalInterface
public interface IntPredicate
Methods of IntPredicate Interface
1. test() Method
The test() method is the functional method of the IntPredicate interface. It evaluates a condition on the given integer 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(int value)
Parameters: value- The input integer value.
Return Value: Returns true if the condition is satisfied; otherwise false.
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
IntPredicate isPositive = n -> n > 0;
System.out.println(isPositive.test(15));
}
}
Output
true
Explanation: The predicate checks whether a number is positive. Since 15 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 IntPredicate.
- Supports predicate chaining.
Syntax:
default IntPredicate and(IntPredicate other)
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
IntPredicate greaterThan10 = n -> n > 10;
IntPredicate 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 IntPredicate or(IntPredicate other)
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
IntPredicate lessThan0 = n -> n < 0;
IntPredicate 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 IntPredicate negate()
import java.util.function.IntPredicate;
public class Main {
public static void main(String[] args) {
IntPredicate isPositive = n -> n > 0;
System.out.println(
isPositive.negate().test(-5)
);
}
}
Output
true
Explanation: negate() reverses the condition, so it returns true for a negative number.
Example: Java example to demonstrate IntPredicate interface
import java.util.function.IntPredicate;
public class IntPredicateDemo {
public static void main(String[] args)
{
// Predicate to check a value is less than 544331
IntPredicate intPredicate = (x) ->
{
if (x <= 544331)
return true;
return false;
};
System.out.println("544331 is less than 544331 "
+ intPredicate.test(544331));
// Predicate to check a value is equal to 544331
IntPredicate predicate = (x) ->
{
if (x == 544331)
return true;
return false;
};
System.out.println("544331 is equal to 544331 "
+ predicate.test(544331));
// ORing the two predicates
IntPredicate intPredicate1 = intPredicate.or(predicate);
System.out.println("544331 is less than equal to 544331 "
+ intPredicate1.test(544331));
// ANDing the two predicates
intPredicate1 = intPredicate.and(predicate);
System.out.println("544331 is equal to 544331 "
+ intPredicate1.test(544331));
// Negating the predicate
intPredicate1 = intPredicate.negate();
System.out.println("544331 is greater than 544331 "
+ intPredicate1.test(544331));
}
}
Output
544331 is less than 544331 true 544331 is equal to 544331 true 544331 is less than equal to 544331 true 544331 is equal to 544331 true 544331 is greater than 544331 false
IntPredicate vs Predicate<Integer>
| Feature | IntPredicate | Predicate |
|---|---|---|
| Input Type | int | Integer |
| Return Type | boolean | boolean |
| Boxing/Unboxing | Not Required | Required |
| Performance | Faster | Slightly Slower |
| Package | java.util.function | java.util.function |