-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPredicateSearch.java
More file actions
35 lines (24 loc) · 840 Bytes
/
PredicateSearch.java
File metadata and controls
35 lines (24 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package ocp.chapter.six;
import java.util.*;
import java.util.function.*;
// p.230
// Predicate interface - Good for conditional statements (returns boolean).
// public interface Predicate<T> {
// boolean test(T t);
// }
public class PredicateSearch {
public static void main(String... args) {
List<Animal> animals = new ArrayList<>();
animals.add(new Animal("Fish", true));
Predicate<Animal> predicate = a -> a.canHop();
print(animals, predicate);
// print(animals, a -> a.canHop()); // Works too.
}
private static void print(List<Animal> animals, Predicate<Animal> checker) {
for (Animal animal : animals) {
if (checker.test(animal)) // a -> a.canHop()
System.out.println(animal + " ");
}
System.out.println();
}
}