|
16 | 16 | */
|
17 | 17 | public class Lambda3 {
|
18 | 18 |
|
19 |
| - @FunctionalInterface |
20 |
| - interface Fun { |
21 |
| - void foo(); |
22 |
| - } |
| 19 | + @FunctionalInterface |
| 20 | + interface Fun { |
| 21 | + void foo(); |
| 22 | + } |
23 | 23 |
|
24 |
| - public static void main(String[] args) throws Exception { |
| 24 | + public static void main(String[] args) throws Exception { |
25 | 25 |
|
26 |
| - // Predicates |
| 26 | + // Predicates |
| 27 | + Predicate<String> predicate = (s) -> s.length() > 0; |
27 | 28 |
|
28 |
| - Predicate<String> predicate = (s) -> s.length() > 0; |
| 29 | + predicate.test("foo"); // true |
| 30 | + predicate.negate().test("foo"); // false |
29 | 31 |
|
30 |
| - predicate.test("foo"); // true |
31 |
| - predicate.negate().test("foo"); // false |
| 32 | + Predicate<Boolean> nonNull = Objects::nonNull; |
| 33 | + nonNull.test(null); |
32 | 34 |
|
33 |
| - Predicate<Boolean> nonNull = Objects::nonNull; |
34 |
| - Predicate<Boolean> isNull = Objects::isNull; |
| 35 | + Predicate<Boolean> isNull = Objects::isNull; |
| 36 | + isNull.test(null); |
35 | 37 |
|
36 |
| - Predicate<String> isEmpty = String::isEmpty; |
37 |
| - Predicate<String> isNotEmpty = isEmpty.negate(); |
| 38 | + Predicate<String> isEmpty = String::isEmpty; |
| 39 | + isEmpty.test(""); |
38 | 40 |
|
| 41 | + Predicate<String> isNotEmpty = isEmpty.negate(); |
| 42 | + isNotEmpty.test(""); |
| 43 | + |
| 44 | + // Functions |
| 45 | + Function<String, Integer> toInteger = Integer::valueOf; |
| 46 | + Function<String, String> backToString = toInteger.andThen(String::valueOf); |
39 | 47 |
|
40 |
| - // Functions |
| 48 | + backToString.apply("123"); // "123" |
41 | 49 |
|
42 |
| - Function<String, Integer> toInteger = Integer::valueOf; |
43 |
| - Function<String, String> backToString = toInteger.andThen(String::valueOf); |
| 50 | + // Suppliers |
44 | 51 |
|
45 |
| - backToString.apply("123"); // "123" |
| 52 | + Supplier<Person> personSupplier = Person::new; |
| 53 | + personSupplier.get(); // new Person |
46 | 54 |
|
| 55 | + // Consumers |
47 | 56 |
|
48 |
| - // Suppliers |
| 57 | + Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); |
| 58 | + greeter.accept(new Person("Luke", "Skywalker")); |
49 | 59 |
|
50 |
| - Supplier<Person> personSupplier = Person::new; |
51 |
| - personSupplier.get(); // new Person |
| 60 | + // Comparators |
52 | 61 |
|
| 62 | + Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); |
53 | 63 |
|
54 |
| - // Consumers |
| 64 | + Person p1 = new Person("John", "Doe"); |
| 65 | + Person p2 = new Person("Alice", "Wonderland"); |
55 | 66 |
|
56 |
| - Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); |
57 |
| - greeter.accept(new Person("Luke", "Skywalker")); |
| 67 | + comparator.compare(p1, p2); // > 0 |
| 68 | + comparator.reversed().compare(p1, p2); // < 0 |
58 | 69 |
|
| 70 | + // Runnables |
59 | 71 |
|
| 72 | + Runnable runnable = () -> System.out.println(UUID.randomUUID()); |
| 73 | + runnable.run(); |
60 | 74 |
|
61 |
| - // Comparators |
| 75 | + // Callables |
62 | 76 |
|
63 |
| - Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); |
64 |
| - |
65 |
| - Person p1 = new Person("John", "Doe"); |
66 |
| - Person p2 = new Person("Alice", "Wonderland"); |
67 |
| - |
68 |
| - comparator.compare(p1, p2); // > 0 |
69 |
| - comparator.reversed().compare(p1, p2); // < 0 |
70 |
| - |
71 |
| - |
72 |
| - // Runnables |
73 |
| - |
74 |
| - Runnable runnable = () -> System.out.println(UUID.randomUUID()); |
75 |
| - runnable.run(); |
76 |
| - |
77 |
| - |
78 |
| - // Callables |
79 |
| - |
80 |
| - Callable<UUID> callable = UUID::randomUUID; |
81 |
| - callable.call(); |
82 |
| - } |
| 77 | + Callable<UUID> callable = UUID::randomUUID; |
| 78 | + callable.call(); |
| 79 | + } |
83 | 80 |
|
84 | 81 | }
|
0 commit comments