PredicateApplies a test to an object and returns the result.
Typically used for filter operations on a Collection or Stream.
Parameterized by the type contained in the Collection or Stream.
Method to be implemented is boolean test(T t). Thus, a lambda that implements Predicate must take a single parameter and return a boolean result.
Collection using Collection.removeIf(Predicate):Collection<Integer> data = new LinkedList<>(List.of(1, 1, 2, 3, 5, 8, 13));
data.removeIf((v) -> v % 2 == 0); // Remove even values.
// data contains [1, 1, 3, 5, 13]
Stream using Stream.filter(Predicate):List<Integer> data = Stream.of(1, 1, 2, 3, 5, 8, 13)
.filter((v) -> v % 2 == 1) // Preserve odd values.
.collect(Collectors.toList());
// data contains [1, 1, 3, 5, 13]
Note that the sense of the predicate is reversed in the two uses shown above: Collection.removeIf(Predicate) removes an item from the collection if (and only if) the predicate evaluates to true for that item; Stream.filter(Predicate) preserves an item in the stream if (and only if) the predicate evaluates to true for that item.