To use the IntPredicate
functional interface in Java, the approach and structure are similar to IntFunction
but with key differences in its purpose.
What is IntPredicate
?
The IntPredicate
functional interface belongs to the java.util.function
package. It represents a predicate (boolean-valued function) that takes a single int
input. This interface is particularly useful when working with primitive int
data to avoid autoboxing and unboxing overhead compared to using the generic Predicate<Integer>
interface.
Functional Interface Definition
The IntPredicate
interface is defined as:
@FunctionalInterface
public interface IntPredicate {
boolean test(int value);
}
- Method:
boolean test(int value)
- Accepts a single
int
as input. - Returns a
boolean
result.
- Accepts a single
How to Use IntPredicate
We can implement this interface using:
1. Lambda expressions.
2. Method references.
3. Anonymous classes.
Example 1: Using a Lambda Expression
package org.kodejava.util.function;
import java.util.function.IntPredicate;
public class IntPredicateExample {
public static void main(String[] args) {
// Define an IntPredicate to check if a number is even
IntPredicate isEven = num -> num % 2 == 0;
// Test the predicate
// Output: true
System.out.println(isEven.test(4));
// Output: false
System.out.println(isEven.test(5));
}
}
Example 2: Using a Method Reference
We can also refer to a method that matches the signature of boolean test(int value)
.
package org.kodejava.util.function;
import java.util.function.IntPredicate;
public class MethodRefExample {
public static void main(String[] args) {
// Use a static method reference
IntPredicate isPositive = MethodRefExample::isPositive;
// Test the predicate
// Output: true
System.out.println(isPositive.test(10));
// Output: false
System.out.println(isPositive.test(-5));
}
// A static method compatible with IntPredicate
public static boolean isPositive(int num) {
return num > 0;
}
}
Example 3: Anonymous Class Implementation
Here’s how we can implement IntPredicate
using an anonymous class.
package org.kodejava.util.function;
import java.util.function.IntPredicate;
public class IntPredicateAnonymousExample {
public static void main(String[] args) {
// Anonymous class implementation
IntPredicate isNegative = new IntPredicate() {
@Override
public boolean test(int value) {
return value < 0;
}
};
// Test the predicate
// Output: true
System.out.println(isNegative.test(-3));
// Output: false
System.out.println(isNegative.test(2));
}
}
Using IntPredicate
in Streams
IntPredicate is particularly common with IntStream
operations to filter primitive integers based on conditions.
Example: Filter Even Numbers from an IntStream
package org.kodejava.util.function;
import java.util.function.IntPredicate;
import java.util.stream.IntStream;
public class IntStreamExample {
public static void main(String[] args) {
IntPredicate isEven = num -> num % 2 == 0;
// Use IntPredicate in a stream
IntStream.range(1, 10)
.filter(isEven) // Filter only even numbers
.forEach(System.out::println);
// Output: 2, 4, 6, 8
}
}
Key Benefits of IntPredicate
- Avoid Autoboxing Overhead: The
IntPredicate
works with primitiveint
, avoiding the boxing/unboxing required withPredicate<Integer>
. - Functional Programming Support: Perfect for use with functional-style code, especially in streams.
- Simplified Syntax: Cleaner syntax for filtering or applying boolean conditions directly on
int
values.
Summary
IntPredicate
is a functional interface that accepts anint
and returns aboolean
.- It is typically used in lambda expressions, method references, and streams.
- Avoids the overhead of boxing and unboxing, leading to better performance when working with primitives.
By using IntPredicate
, we can efficiently handle boolean logic operations for primitive integers in Java.