java.util.function
package.
and()
, or()
, and negate()
.public interface Predicate
{
boolean test(T t);
//some default methods are also present
}
import java.util.function.Predicate;
public class MainApp
{
public static void main(String[] args)
{
// Predicate to check if a number is even using anonymous class
Predicate<Integer> isEven = new Predicate<Integer>() {
@Override
public boolean test(Integer x) {
return x % 2 == 0;
}
};
// Test numbers
System.out.println("Is 4 is even : " + isEven.test(4));
System.out.println("Is 7 is even : " + isEven.test(7));
}
}
Is 4 is even : true Is 7 is even : false
import java.util.function.Predicate;
public class MainApp
{
public static void main(String[] args)
{
// Predicate to check if a number is even using lambda expression
Predicate<Integer> isEven = x -> x % 2 == 0;
// Test numbers
System.out.println("Is 4 even? " + isEven.test(4));
System.out.println("Is 7 even? " + isEven.test(7));
}
}
Is 4 is even : true Is 7 is even : false
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.