September 2019
Intermediate to advanced
816 pages
18h 47m
English
Let's assume that we have the following Melon class and List of Melon:
public class Melon { private final String type; private final int weight; // constructors, getters, setters, equals(), // hashCode(), toString() omitted for brevity}List<Melon> melons = Arrays.asList(new Melon("Gac", 2000), new Melon("Horned", 1600), new Melon("Apollo", 3000), new Melon("Gac", 3000), new Melon("Hemi", 1600));
The Predicate interface comes with three methods that take a Predicate and uses it to obtain an enriched Predicate. These methods are and(), or(), and negate().
For example, let's assume that we want to filter the melons that are heavier than 2,000 g. For this, we can write a Predicate, as follows:
Predicate<Melon> p2000 = m -> ...