-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo2.java
195 lines (155 loc) · 6.14 KB
/
demo2.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package Advances.StreamAPI;
// https://www.youtube.com/watch?v=vL23nxrQWuI&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=678
// https://www.youtube.com/watch?v=jPjOW6f1_EA&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=678
// https://www.youtube.com/watch?v=DlHnbbS3bBY&list=PLmOn9nNkQxJH0qBIrtV6otI0Ep4o2q67A&index=680
import Advances.Lambda.demo4.Employee;
import Advances.Lambda.demo4.EmployeeData;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
/**
* Stream API demo 2
*
* <p>Stream intermedia op
*
* <p>1) filter, sampling 2) mapping 3) ordering
*/
public class demo2 {
/** transform char in string to stream */
public static Stream<Character> fromStringToStream(String str) {
ArrayList<Character> list = new ArrayList<>();
for (Character c : str.toCharArray()) {
list.add(c);
}
return list.stream();
}
/**
* demo 1 : -> filter(Predicate p) : accept Lambda, filter elements from stream -> limit(n) : only
* get element with limit count -> skip(n) : neglect elements : return a stream neglect first n
* elements -> distinct : via hashCode(), and equals() de-duplicate elements and return
*/
@Test
public void test1() {
// init
List<Employee> list = EmployeeData.getEmployees();
Stream<Employee> stream = list.stream();
// filter
stream.filter(e -> e.getSalary() > 100).forEach(System.out::println);
System.out.println("================");
// limit
// below is wrong, CAN'T do limit op on already closed stream (java.lang.IllegalStateException:
// stream has already been operated upon or closed)
// stream.limit(2).forEach(System.out::println);
list.stream().limit(2).forEach(System.out::println);
System.out.println("================");
// skip
// below is wrong, CAN'T do limit op on already closed stream (java.lang.IllegalStateException:
// stream has already been operated upon or closed)
// stream.skip(2).forEach(System.out::println);
list.stream().skip(3).forEach(System.out::println);
System.out.println("================");
// distinct
// add duplicated data
list.add(new Employee(1001, "jack", 34, 700.1));
list.add(new Employee(1001, "jack", 34, 700.1));
list.add(new Employee(1001, "jack", 34, 700.1));
list.add(new Employee(1001, "jack", 34, 700.1));
list.stream().distinct().forEach(System.out::println);
}
/**
* demo 2 -> map(Function f) : receive a func as param, transform elements with it
*
* <p>-> flatMap(Function f) : receive a func as param, transform elements with it if there are
* sub-streams will put all of them into original stream ("flat")
*/
@Test
public void test2() {
// init
List<String> list = Arrays.asList("AA", "aa", "bb", "cc", "dd");
Stream<String> stream = list.stream();
List<Employee> employees = EmployeeData.getEmployees();
// map demo 1
stream.map(str -> str.toUpperCase()).forEach(System.out::println);
System.out.println("================");
// map demo 2 : filter age > 3
// employees.stream().map(str -> str.getAge() > 3).map(Employee::getName);
Stream<String> namesStream = employees.stream().map(Employee::getName);
namesStream.filter(name -> name.length() > 3).forEach(System.out::println);
System.out.println("================");
// flatMap demo 1
// if still use map (not use flatMap)
Stream<Stream<Character>> streamStream = list.stream().map(demo2::fromStringToStream);
streamStream.forEach(System.out::println);
System.out.println("================");
Stream<Character> characterStream = list.stream().flatMap(demo2::fromStringToStream);
characterStream.forEach(System.out::println);
}
/** Review : -> list.add() VS list.addAll() -> similar as map VS flatMap */
@Test
public void test3() {
ArrayList list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
ArrayList list1_ = new ArrayList<>();
list1_.add(1);
list1_.add(2);
list1_.add(3);
ArrayList list2 = new ArrayList<>();
list2.add(4);
list2.add(5);
list2.add(6);
list1.add(list2);
System.out.println("list1 = " + list1);
list1_.addAll(list2);
System.out.println("list1_ = " + list1_);
}
/**
* Sorting 1) sorted() : natural ordering, generate a new stream, order by natural ordering 2)
* sorted(Comparator com) : custom ordering, generate a new stream, order by Comparator ordering
*/
@Test
public void test4() {
// init
List<Integer> list = Arrays.asList(12, -9, 0, 100, 200, 1000);
Stream stream = list.stream();
// sorted() demo 1
list.stream().sorted().forEach(System.out::println);
System.out.println("================");
// sorted() demo 2
List<Employee> employees = EmployeeData.getEmployees();
// wrong, error : java.lang.ClassCastException: Advances.Lambda.demo4.Employee cannot be cast to
// java.lang.Comparable
// since employees has NO implementation on Comparator interface
// employees.stream().sorted().forEach(System.out::println);
System.out.println("================");
// sorted(Comparator com) demo 1
List<Employee> employees2 = EmployeeData.getEmployees();
employees2.stream()
.sorted(
// implement Comparator with lambda expression
(e1, e2) -> {
return Integer.compare(e1.getAge(), e2.getAge());
})
.forEach(System.out::println);
System.out.println("================");
// sorted(Comparator com) demo 2
List<Employee> employees3 = EmployeeData.getEmployees();
employees2.stream()
.sorted(
// implement Comparator with lambda expression
(e1, e2) -> {
// if age are different, return result directly
int ageVal = Integer.compare(e1.getAge(), e2.getAge());
if (ageVal != 0) {
return ageVal;
} else {
// if age are the same, compare salary instead
return Double.compare(e1.getSalary(), e2.getSalary());
}
})
.forEach(System.out::println);
}
}