In Java 8, Predicate is a functional interface and that can be used as the assignment target for a lambda expression or method reference.
Java 8 provides predefined functional interfaces to deal with functional programming by using lambda and method references.
In this example, we demonstrate the usage of Predicate predefined functional interfaces.
Java Predicate Example
Step 1: Create a Person class, this Person class is used to demonstrate Predefined-Functional Interfaces examples.
public class Person {
private String name;
private int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Here is an example of a Predicate interface:
public class PredicateExample { public static void main(String[] args) { Predicate < Person > predicate = (person) - > person.getAge() > 28; boolean result = predicate.test(new Person("ramesh", 29)); System.out.println(result); } }
Output:
true
Comments
Post a Comment