|
17 | 17 | */ |
18 | 18 | public class LambdaExpressions { |
19 | 19 |
|
20 | | - public static void printPersonsOlderThan(List<Person> roster, int age) { |
| 20 | + |
| 21 | + private void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester) { |
21 | 22 |
|
22 | 23 | for (Person p : roster) { |
23 | | - if (p.getAge() >= age) { |
| 24 | + if (tester.test(p)) { |
24 | 25 | p.printPerson(); |
25 | 26 | } |
26 | | - |
27 | 27 | } |
28 | 28 | } |
29 | 29 |
|
30 | | - public static void printPersonsWithinAgeRange(List<Person> roster, int low, int high) { |
| 30 | + private void processPersons(List<Person> roster, Predicate<Person> tester, Consumer<Person> block) { |
31 | 31 | for (Person p : roster) { |
32 | | - if (low <= p.getAge() && p.getAge() < high) { |
33 | | - p.printPerson(); |
| 32 | + if (tester.test(p)) { |
| 33 | + block.accept(p); |
34 | 34 | } |
35 | 35 | } |
36 | 36 | } |
37 | 37 |
|
38 | | - public static void printPersons(List<Person> roster, CheckPerson tester) { |
39 | | - |
| 38 | + private void processPersonsWithFunction(List<Person> roster, Predicate<Person> tester, Function<Person, String> mapper, Consumer<String> block) { |
40 | 39 | for (Person p : roster) { |
41 | | - if(tester.test(p)){ |
42 | | - p.printPerson(); |
43 | | - } |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - public static void printPersonsWithPredicate(List<Person> roster, Predicate<Person> tester){ |
48 | | - |
49 | | - for (Person p : roster) { |
50 | | - if(tester.test(p)){ |
51 | | - p.printPerson(); |
52 | | - } |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - public static void processPersons(List<Person> roster, Predicate<Person> tester, Consumer<Person> block){ |
57 | | - for (Person p : roster) { |
58 | | - if(tester.test(p)){ |
59 | | - block.accept(p); |
60 | | - } |
61 | | - } |
62 | | - } |
63 | | - |
64 | | - public static void processPersonsWithFunction(List<Person> roster, Predicate<Person> tester,Function<Person, String> mapper, Consumer<String> block){ |
65 | | - for (Person p : roster) { |
66 | | - if(tester.test(p)){ |
| 40 | + if (tester.test(p)) { |
67 | 41 | String data = mapper.apply(p); |
68 | 42 | block.accept(data); |
69 | | - } |
| 43 | + } |
70 | 44 | } |
71 | 45 | } |
72 | | - |
73 | | - public static <X, Y> void processElements(Iterable<X> source, |
74 | | - Predicate<X> tester, |
75 | | - Function<X, Y> mapper, |
76 | | - Consumer<Y> block){ |
77 | | - |
| 46 | + |
| 47 | + private <X, Y> void processElements(Iterable<X> source, |
| 48 | + Predicate<X> tester, |
| 49 | + Function<X, Y> mapper, |
| 50 | + Consumer<Y> block) { |
| 51 | + |
78 | 52 | for (X p : source) { |
79 | | - if(tester.test(p)){ |
| 53 | + if (tester.test(p)) { |
80 | 54 | Y data = mapper.apply(p); |
81 | 55 | block.accept(data); |
82 | | - } |
| 56 | + } |
83 | 57 | } |
84 | | - |
| 58 | + |
85 | 59 | } |
86 | | - |
87 | | - public static void main(String[] args) { |
88 | | - printPersons(new ArrayList<>(), new CheckPersonEligibleForSelectiveService()); |
89 | | - |
90 | | - printPersons(new ArrayList<>(), new CheckPerson() { |
91 | 60 |
|
92 | | - @Override |
93 | | - public boolean test(Person p) { |
94 | | - return p.getGender() == Person.Sex.MALE |
95 | | - && p.getAge() >= 18 |
96 | | - && p.getAge() <= 25; |
97 | | - } |
98 | | - }); |
99 | | - |
100 | | - printPersons(new ArrayList<>(), |
101 | | - (Person p) -> p.getGender() == Person.Sex.MALE |
102 | | - && p.getAge() >= 18 |
103 | | - && p.getAge() <= 25); |
104 | | - |
105 | | - |
106 | | - printPersonsWithPredicate(new ArrayList<>(), |
107 | | - p->p.getGender()==Person.Sex.MALE |
| 61 | + private void processElementsWithAggregateOperationsAndLambdaExpressions(List<Person> roster) { |
| 62 | + |
| 63 | + roster |
| 64 | + .stream() |
| 65 | + .filter(p -> p.getGender() == Person.Sex.MALE |
| 66 | + && p.getAge() >= 18 |
| 67 | + && p.getAge() <= 25) |
| 68 | + .map(p -> p.getEmailAddress()) |
| 69 | + .forEach(email -> System.out.println(email)); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + public void runExamples() { |
| 74 | + |
| 75 | + System.out.println("JDK 8 Lambdas Essentials"); |
| 76 | + |
| 77 | + |
| 78 | + System.out.println("printPersonsWithPredicate"); |
| 79 | + |
| 80 | + System.out.println("printPersonsWithinAgeRange"); |
| 81 | + printPersonsWithPredicate(new ArrayList<>(), |
| 82 | + p -> p.getGender() == Person.Sex.MALE |
108 | 83 | && p.getAge() >= 18 |
109 | 84 | && p.getAge() <= 25 |
110 | 85 | ); |
111 | | - |
| 86 | + |
| 87 | + |
| 88 | + |
112 | 89 | processPersons(new ArrayList<>(), |
113 | | - p->p.getGender()==Person.Sex.MALE |
| 90 | + p -> p.getGender() == Person.Sex.MALE |
114 | 91 | && p.getAge() >= 18 |
115 | | - && p.getAge() <= 25, |
116 | | - p->p.printPerson()); |
117 | | - |
118 | | - |
119 | | - processPersonsWithFunction(new ArrayList<>(), |
120 | | - p->p.getGender()==Person.Sex.MALE |
| 92 | + && p.getAge() <= 25, |
| 93 | + p -> p.printPerson()); |
| 94 | + |
| 95 | + System.out.println("processPersonsWithFunction"); |
| 96 | + |
| 97 | + processPersonsWithFunction(new ArrayList<>(), |
| 98 | + p -> p.getGender() == Person.Sex.MALE |
121 | 99 | && p.getAge() >= 18 |
122 | 100 | && p.getAge() <= 25, |
123 | | - p->p.getEmailAddress(), |
124 | | - email->System.out.println(email) |
125 | | - ); |
126 | | - |
127 | | - processElements(new ArrayList<Person>(), |
128 | | - p->p.getGender()==Person.Sex.MALE |
| 101 | + p -> p.getEmailAddress(), |
| 102 | + email -> System.out.println(email) |
| 103 | + ); |
| 104 | + |
| 105 | + System.out.println("processElements"); |
| 106 | + |
| 107 | + processElements(new ArrayList<Person>(), |
| 108 | + p -> p.getGender() == Person.Sex.MALE |
129 | 109 | && p.getAge() >= 18 |
130 | 110 | && p.getAge() <= 25, |
131 | | - p->p.getEmailAddress(), |
132 | | - email->System.out.println(email) |
133 | | - ); |
134 | | - |
135 | | - List<Person> roster = new ArrayList<>(); |
136 | | - |
137 | | - |
138 | | - roster |
139 | | - .stream() |
140 | | - .filter(p->p.getGender()==Person.Sex.MALE |
141 | | - && p.getAge() >= 18 |
142 | | - && p.getAge() <= 25) |
143 | | - .map(p->p.getEmailAddress()) |
144 | | - .forEach(email->System.out.println(email)); |
145 | | - } |
| 111 | + p -> p.getEmailAddress(), |
| 112 | + email -> System.out.println(email) |
| 113 | + ); |
| 114 | + |
| 115 | + System.out.println("processElements with Aggregate Operations That Accept Lambda Expressions as Parameters"); |
| 116 | + |
| 117 | + processElementsWithAggregateOperationsAndLambdaExpressions(new ArrayList<>()); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + public static void main(String[] args) { |
| 122 | + |
| 123 | + LambdaExpressions lambda = new LambdaExpressions(); |
| 124 | + lambda.runExamples(); |
| 125 | + } |
146 | 126 | } |
0 commit comments