-
Notifications
You must be signed in to change notification settings - Fork 0
/
JEP107BulkDataOperationsForCollections.java
220 lines (182 loc) · 8.55 KB
/
JEP107BulkDataOperationsForCollections.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class JEP107BulkDataOperationsForCollections {
public static void main(String[] args) {
// https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
// JEP 107: Bulk Data Operations for Collections
// https://openjdk.org/jeps/107
Stream<String> streamEmpty = Stream.empty(); // empty stream
// map method
System.out.println("\n------map method------\n");
List<Integer> numbers = Arrays.asList(1,3,5,7,9);
numbers.stream().map(x-> x*2).collect(Collectors.toList()).forEach(System.out::println);
// allMatch method
System.out.println("\n------allMatch method------\n");
boolean hasUnevenNumber = numbers.stream().allMatch(n-> n % 2 == 0);
String arrays = numbers.stream().map(x-> x.toString()).collect(Collectors.joining(","));
System.out.println(String.format("There are even numbers (%b) in that array (%s)", hasUnevenNumber, arrays));
// filter method
System.out.println("\n------filter method------\n");
List<String> names = Arrays.asList("Reflection", "Collection", "Stream");
names.stream().filter(x-> x.contains("i")).collect(Collectors.toList()).forEach(System.out::println);
// sorted method
// https://stackoverflow.com/questions/40517977/sorting-a-list-with-stream-sorted-in-java
System.out.println("\n------sorted method------\n");
names.stream().sorted(Comparator.comparingLong(String::length)).collect(Collectors.toList()).forEach(System.out::println);
List<Person> people = Arrays.asList(
new Person("Barbara Liskov", 1939 ),
new Person("Grace Hopper", 1906),
new Person("Dennis Ritchie", 1941),
new Person("Donald Knuth", 1938 ),
new Person("Edsger W. Dijkstra", 1930),
new Person("James Gosling", 1955 )
);
// people.stream().sorted(Comparator.comparingInt(Person::getBornOfYear).reversed()).forEach(System.out::println);
people.stream().sorted(Comparator.comparingInt(Person::getBornOfYear)).forEach(System.out::println);
// count method
System.out.println("\n------count method------\n");
long count = people.stream().filter(p-> p.getBornOfYear() >1940).count();
System.out.println(String.format("%d people were born after %d", count, 1940));
// iterate, skip & limit methods
System.out.println("\n------iterate, skip & limit methods------\n");
Stream<Integer> infiniteStream = Stream.iterate(100, i -> i*5);
infiniteStream.skip(10).limit(5).forEach(System.out::println);
// generate method
System.out.println("\n------generate method------\n");
Stream.generate(Math::random).limit(10).forEach(System.out::println);
// findFirst method (return by Optional type)
System.out.println("\n------findFirst method (return by Optional type)------\n");
Person selectedPerson = people.stream().filter(x-> x.getBornOfYear() == 1938).findFirst().orElse(null);
System.out.println(selectedPerson);
// min method
System.out.println("\n------min method------\n");
Person minBornOfYear = people.stream().min(Comparator.comparingInt(Person::getBornOfYear)).orElse(null);
System.out.println(minBornOfYear);
// max method
System.out.println("\n------min method------\n");
Person maxBornOfYear = people.stream().max(Comparator.comparingInt(Person::getBornOfYear)).orElse(null);
System.out.println(maxBornOfYear);
// distinct method
System.out.println("\n------distinct method------\n");
System.out.println("Example 1:\n");
List<Integer> duplicatedNumbers = Arrays.asList(1,2,2,5,7,8,9,2,6,7,10,11,1,23,78,7);
duplicatedNumbers.stream().distinct().sorted().forEach(System.out::println);
System.out.println("\nExample 2:\n");
// https://stackoverflow.com/questions/23699371/java-8-distinct-by-property
List<Scientist> duplicatedScientists = Arrays.asList(
new Scientist("Barbara Liskov"),
new Scientist("Grace Hopper"),
new Scientist("Dennis Ritchie"),
new Scientist("Donald Knuth"),
new Scientist("Edsger W. Dijkstra"),
new Scientist("James Gosling"),
new Scientist("Grace Hopper"),
new Scientist("Dennis Ritchie"),
new Scientist("Donald Knuth"),
new Scientist("Edsger W. Dijkstra"),
new Scientist("Grace Hopper"),
new Scientist("Dennis Ritchie"),
new Scientist("Donald Knuth"),
new Scientist("Edsger W. Dijkstra")
);
duplicatedScientists.stream().distinct().forEach(System.out::println);
// Collectors.joining & map methods
System.out.println("\n------Collectors.joining & map methods------\n");
String computerScientistNames = people.stream().map(Person::getName).collect(Collectors.joining(","));
System.out.println(computerScientistNames);
// Collectors.toSet methods (same distinct)
System.out.println("\n------Collectors.toSet method------\n");
duplicatedNumbers.stream().collect(Collectors.toSet()).forEach(System.out::println);
// map (with model mapping)
// https://stackoverflow.com/questions/54834550/simplified-java-8-stream-map-entity-to-model
System.out.println("\n------map (with model mapping) method------\n");
people.stream().map(person -> {
return new Scientist(person.getName());
}).forEach(System.out::println);
List<Book> books = Arrays.asList(
new Book("Effective Java", 32.47),
new Book("Clean Code", 44.99),
new Book("The Pragmatic Programmer", 30.18),
new Book("The Clean Coder", 37.29),
new Book("Working Effectively with Legacy Code", 31.47)
);
// DoubleSummaryStatistics
// https://stackoverflow.com/questions/3693079/problem-with-system-out-printf-command-in-java
System.out.println("\n------DoubleSummaryStatistics------\n");
DoubleSummaryStatistics statistics = books.stream().collect(Collectors.summarizingDouble(Book::getUnitPrice));
System.out.println(String.format("Book count: %d", statistics.getCount()));
System.out.println(String.format("Book total unit price: %f", statistics.getSum()));
System.out.println(String.format("Book max unit price: %f", statistics.getMax()));
System.out.println(String.format("Book min unit price: %f", statistics.getMin()));
System.out.println(String.format("Book Average unit price: %f", statistics.getAverage()));
}
static Stream<String> streamOf(List<String> items) {
return items == null || items.isEmpty() ? Stream.empty() : items.stream();
}
}
class Person {
private final String name;
private final Integer bornOfYear;
public Person(String name, Integer bornOfYear) {
this.name = name;
this.bornOfYear = bornOfYear;
}
public String getName() {
return name;
}
public Integer getBornOfYear() {
return bornOfYear;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", bornOfYear=" + bornOfYear +
'}';
}
}
class Scientist {
private String name;
public Scientist(String name) {
this.name = name;
}
@Override
public String toString() {
return "Scientist{" +
"name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Scientist scientist = (Scientist) o;
return Objects.equals(name, scientist.name);
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}
class Book {
private String name;
private Double unitPrice;
public Book(String name, Double unitPrice) {
this.name = name;
this.unitPrice = unitPrice;
}
public String getName() {
return name;
}
public Double getUnitPrice() {
return unitPrice;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", unitPrice=" + unitPrice +
'}';
}
}