|
1 | | -package com.winterbe.java8.samples.lambda; |
2 | | - |
3 | | -import java.util.Comparator; |
4 | | -import java.util.Objects; |
5 | | -import java.util.UUID; |
6 | | -import java.util.concurrent.Callable; |
7 | | -import java.util.function.Consumer; |
8 | | -import java.util.function.Function; |
9 | | -import java.util.function.Predicate; |
10 | | -import java.util.function.Supplier; |
11 | | - |
12 | | -/** |
13 | | - * Common standard functions from the Java API. |
14 | | - * |
15 | | - * @author Benjamin Winterberg |
16 | | - */ |
17 | | -public class Lambda3 { |
18 | | - |
19 | | - @FunctionalInterface |
20 | | - interface Fun { |
21 | | - void foo(); |
22 | | - } |
23 | | - |
24 | | - public static void main(String[] args) throws Exception { |
25 | | - |
26 | | - // Predicates |
27 | | - Predicate<String> predicate = (s) -> s.length() > 0; |
28 | | - |
29 | | - predicate.test("foo"); // true |
30 | | - predicate.negate().test("foo"); // false |
31 | | - |
32 | | - Predicate<Boolean> nonNull = Objects::nonNull; |
33 | | - nonNull.test(null); |
34 | | - |
35 | | - Predicate<Boolean> isNull = Objects::isNull; |
36 | | - isNull.test(null); |
37 | | - |
38 | | - Predicate<String> isEmpty = String::isEmpty; |
39 | | - isEmpty.test(""); |
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); |
47 | | - |
48 | | - backToString.apply("123"); // "123" |
49 | | - |
50 | | - // Suppliers |
51 | | - |
52 | | - Supplier<Person> personSupplier = Person::new; |
53 | | - personSupplier.get(); // new Person |
54 | | - |
55 | | - // Consumers |
56 | | - |
57 | | - Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); |
58 | | - greeter.accept(new Person("Luke", "Skywalker")); |
59 | | - |
60 | | - // Comparators |
61 | | - |
62 | | - Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); |
63 | | - |
64 | | - Person p1 = new Person("John", "Doe"); |
65 | | - Person p2 = new Person("Alice", "Wonderland"); |
66 | | - |
67 | | - comparator.compare(p1, p2); // > 0 |
68 | | - comparator.reversed().compare(p1, p2); // < 0 |
69 | | - |
70 | | - // Runnables |
71 | | - |
72 | | - Runnable runnable = () -> System.out.println(UUID.randomUUID()); |
73 | | - runnable.run(); |
74 | | - |
75 | | - // Callables |
76 | | - |
77 | | - Callable<UUID> callable = UUID::randomUUID; |
78 | | - callable.call(); |
79 | | - } |
80 | | - |
81 | | -} |
| 1 | +package com.winterbe.java8.samples.lambda; |
| 2 | + |
| 3 | +import java.util.Comparator; |
| 4 | +import java.util.Objects; |
| 5 | +import java.util.UUID; |
| 6 | +import java.util.concurrent.Callable; |
| 7 | +import java.util.function.Consumer; |
| 8 | +import java.util.function.Function; |
| 9 | +import java.util.function.Predicate; |
| 10 | +import java.util.function.Supplier; |
| 11 | + |
| 12 | +/** |
| 13 | + * Common standard functions from the Java API. |
| 14 | + * |
| 15 | + * @author Benjamin Winterberg |
| 16 | + */ |
| 17 | +public class CommonFunctions { |
| 18 | + |
| 19 | + @FunctionalInterface |
| 20 | + interface Fun { |
| 21 | + void foo(); |
| 22 | + } |
| 23 | + |
| 24 | + public static void main(String[] args) throws Exception { |
| 25 | + |
| 26 | + // Predicates |
| 27 | + Predicate<String> predicate = (s) -> s.length() > 0; |
| 28 | + |
| 29 | + predicate.test("foo"); // true |
| 30 | + predicate.negate().test("foo"); // false |
| 31 | + |
| 32 | + Predicate<Boolean> nonNull = Objects::nonNull; |
| 33 | + nonNull.test(null); |
| 34 | + |
| 35 | + Predicate<Boolean> isNull = Objects::isNull; |
| 36 | + isNull.test(null); |
| 37 | + |
| 38 | + Predicate<String> isEmpty = String::isEmpty; |
| 39 | + isEmpty.test(""); |
| 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); |
| 47 | + |
| 48 | + backToString.apply("123"); // "123" |
| 49 | + |
| 50 | + // Suppliers |
| 51 | + |
| 52 | + Supplier<Person> personSupplier = Person::new; |
| 53 | + personSupplier.get(); // new Person |
| 54 | + |
| 55 | + // Consumers |
| 56 | + |
| 57 | + Consumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName); |
| 58 | + greeter.accept(new Person("Luke", "Skywalker")); |
| 59 | + |
| 60 | + // Comparators |
| 61 | + |
| 62 | + Comparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName); |
| 63 | + |
| 64 | + Person p1 = new Person("John", "Doe"); |
| 65 | + Person p2 = new Person("Alice", "Wonderland"); |
| 66 | + |
| 67 | + comparator.compare(p1, p2); // > 0 |
| 68 | + comparator.reversed().compare(p1, p2); // < 0 |
| 69 | + |
| 70 | + // Runnables |
| 71 | + |
| 72 | + Runnable runnable = () -> System.out.println(UUID.randomUUID()); |
| 73 | + runnable.run(); |
| 74 | + |
| 75 | + // Callables |
| 76 | + |
| 77 | + Callable<UUID> callable = UUID::randomUUID; |
| 78 | + callable.call(); |
| 79 | + } |
| 80 | +} |
0 commit comments