Predicate represents a predicate, which is boolean-valued function, of one argument.
The following example shows how to use Predicate
.
import java.util.function.Predicate; /*from ww w. j av a 2 s. c om*/ public class Main { public static void main(String[] args) { Predicate<String> i = (s)-> s.length() > 5; System.out.println(i.test("java2s.com ")); } }
The code above generates the following result.
The following code shows how to create Predicate from method reference and lambda.
import java.util.*; import java.util.function.Predicate; //w w w . j a v a 2 s . c om public class Main { public static void main(String... args) { List<Box> inventory = Arrays.asList(new Box(80, "green"), new Box( 155, "green"), new Box(120, "red")); List<Box> greenApples = filter(inventory,Main::isGreenApple); System.out.println(greenApples); List<Box> heavyApples = filter(inventory,Main::isHeavyApple); System.out.println(heavyApples); List<Box> greenApples2 = filter(inventory,(Box a) -> "green".equals(a.getColor())); System.out.println(greenApples2); List<Box> heavyApples2 = filter(inventory,(Box a) -> a.getWeight() > 150); System.out.println(heavyApples2); List<Box> weirdApples = filter(inventory,(Box a) -> a.getWeight() < 80 || "brown".equals(a.getColor())); System.out.println(weirdApples); } public static boolean isGreenApple(Box apple) { return "green".equals(apple.getColor()); } public static boolean isHeavyApple(Box apple) { return apple.getWeight() > 150; } public static List<Box> filter(List<Box> inventory, Predicate<Box> p) { List<Box> result = new ArrayList<>(); for (Box apple : inventory) { if (p.test(apple)) { result.add(apple); } } return result; } } class Box { private int weight = 0; private String color = ""; public Box(int weight, String color) { this.weight = weight; this.color = color; } public Integer getWeight() { return weight; } public void setWeight(Integer weight) { this.weight = weight; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String toString() { return "Apple{" + "color='" + color + '\'' + ", weight=" + weight + '}'; } }
The code above generates the following result.
The following code shows how to return Predicate.
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; import java.util.function.Predicate; //www . ja va 2 s .c om public class Main { public static void main(String[] args) { List<Student> employees = Arrays.asList( new Student(1, 3, "John"), new Student(2, 3, "Jane"), new Student(3, 4, "Jack") ); // with predicate System.out.println(findStudents(employees, createCustomPredicateWith(10_000))); // with function definition, both are same Function<Double, Predicate<Student>> customFunction = threshold -> (e -> e.gpa > threshold); System.out.println(findStudents(employees, customFunction.apply(10_000D))); } private static Predicate<Student> createCustomPredicateWith(double threshold) { return e -> e.gpa > threshold; } private static List<Student> findStudents(List<Student> employees, Predicate<Student> condition) { List<Student> result = new ArrayList<>(); for (Student e : employees) { if (condition.test(e)) { result.add(e); } } return result; } } class Student { public int id; public long gpa; public String name; Student(int id, long g, String name) { this.id = id; this.gpa = g; this.name = name; } @Override public String toString() { return id + ">" + name + ": " + gpa; } }
The code above generates the following result.