forked from itguang/java8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest5.java
More file actions
327 lines (243 loc) · 8.68 KB
/
Copy pathTest5.java
File metadata and controls
327 lines (243 loc) · 8.68 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import com.sun.istack.internal.NotNull;
import org.junit.Before;
import org.junit.Test;
import pojo.CaloricLevel;
import pojo.Currency;
import pojo.Dish;
import pojo.Trader;
import pojo.Transaction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.*;
/**
* @author itguang
* @create 2017-11-16 16:00
**/
public class Test5 {
List<Dish> menu = Dish.menu;
public static List<Transaction> transactions;
@Before
public void before() {
Trader raoul = new Trader("Raoul", "Cambridge");
Trader mario = new Trader("Mario", "Milan");
Trader alan = new Trader("Alan", "Cambridge");
Trader brian = new Trader("Brian", "Cambridge");
transactions = Arrays.asList(
new Transaction(Currency.EUR, 1500.0, brian),
new Transaction(Currency.USD, 2300.0, raoul),
new Transaction(Currency.GBP, 9900.0, mario),
new Transaction(Currency.EUR, 1100.0, alan),
new Transaction(Currency.JPY, 7800.0, brian),
new Transaction(Currency.CHF, 6700.0, brian),
new Transaction(Currency.EUR, 5600.0, mario),
new Transaction(Currency.USD, 4500.0, mario),
new Transaction(Currency.CHF, 3400.0, brian),
new Transaction(Currency.GBP, 3200.0, alan),
new Transaction(Currency.USD, 4600.0, alan),
new Transaction(Currency.JPY, 5700.0, alan),
new Transaction(Currency.EUR, 6800.0, raoul));
}
@Test
public void test1() {
//一次打印
System.out.println(transactions.toString());
//Lambda循环打印
transactions.stream()
.forEach(System.out::println);
}
//用指令式风格对交易按照货币分组
@Test
public void test2() {
Map<Currency, List<Transaction>> transactionsByCurrencies =
new HashMap<>();
List<Transaction> transactionsForCurrency = null;
for (Transaction transaction : transactions) {
Currency currency = transaction.getCurrency();
transactionsForCurrency = transactionsByCurrencies.get(currency);
if (transactionsForCurrency == null) {
transactionsForCurrency = new ArrayList<>();
transactionsByCurrencies
.put(currency, transactionsForCurrency);
}
transactionsForCurrency.add(transaction);
}
//遍历map
for (Currency currency : transactionsByCurrencies.keySet()) {
List<Transaction> transactions = transactionsByCurrencies.get(currency);
System.out.println(transactions);
}
}
@Test
public void test3() {
Map<Currency, List<Transaction>> listMap = transactions.stream()
.collect(Collectors.groupingBy(Transaction::getCurrency));
//遍历map
for (Currency currency : listMap.keySet()) {
System.out.println(listMap.get(currency));
}
}
@Test
public void test4() {
Long aLong = menu.stream()
.collect(Collectors.counting());
System.out.println(aLong);
long count = menu.stream()
.count();
System.out.println(count);
}
@Test
public void test5() {
Long aLong = menu.stream()
.collect(counting());
System.out.println(aLong);
long count = menu.stream()
.count();
System.out.println(count);
}
//找出caid菜单里卡路里的最大值和最小值
@Test
public void test6() {
//使用收集器的方法
Optional<Dish> max = menu.stream()
.collect(maxBy(Comparator.comparing(Dish::getCalories)));
System.out.println(max);
Optional<Dish> min = menu.stream()
.collect(minBy(Comparator.comparing(Dish::getCalories)));
System.out.println(min);
//使用流的方法
Optional<Dish> max1 = menu.stream()
.max(Comparator.comparing(Dish::getCalories));
System.out.println(max1);
Optional<Dish> min1 = menu.stream()
.min(Comparator.comparing(Dish::getCalories));
System.out.println(min1);
}
//汇总
@Test
public void test7() {
//使用收集器汇总
Integer sum = menu.stream()
.collect(summingInt(Dish::getCalories));
System.out.println(sum);
//使用流汇总
Integer sum2 = menu.stream()
.map(Dish::getCalories)
.reduce(0, Integer::sum);
System.out.println(sum2);
}
//汇总
@Test
public void test8() {
IntSummaryStatistics summaryStatistics = menu.stream()
.collect(summarizingInt(Dish::getCalories));
System.out.println(summaryStatistics);
System.out.println(summaryStatistics.getAverage());
}
//连接字符串 joining
@Test
public void test9() {
String names = menu.stream()
.map(Dish::getName)
.collect(joining());
System.out.println(names);
String names2 = menu.stream()
.map(Dish::getName)
.collect(joining(", "));
System.out.println(names2);
}
//使用reducing求和,以不同的方法执行同样的操作
@Test
public void test10() {
//使用收集器汇总
Integer sum = menu.stream()
.collect(summingInt(Dish::getCalories));
System.out.println(sum);
//使用流汇总
Integer sum2 = menu.stream()
.map(Dish::getCalories)
.reduce(0, Integer::sum);
System.out.println(sum2);
//把流映射到一个 IntStream ,然后调用 sum 方法
int sum1 = menu.stream()
.mapToInt(Dish::getCalories)
.sum();
System.out.println(sum1);
Integer sum3 = menu.stream()
.collect(reducing(0,//初始值
Dish::getCalories,//转换函数
Integer::sum));//累积函数
System.out.println(sum3);
}
//使用reducing求最大值,以不同的方法执行同样的操作
@Test
public void test11() {
Optional<Dish> dishOptional = menu.stream()
.collect(reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2));
System.out.println(dishOptional);
}
//分组操作
@Test
public void test12() {
Map<Dish.Type, List<Dish>> typeListMap = menu.stream()
.collect(groupingBy(Dish::getType));
System.out.println(typeListMap);
for (Dish.Type key : typeListMap.keySet()) {
System.out.println(typeListMap.get(key));
}
}
//多级分组操作
@Test
public void test13() {
Map<Dish.Type, Map<CaloricLevel, List<Dish>>> map = menu.stream().collect(
groupingBy(Dish::getType,
groupingBy(dish -> {
if (dish.getCalories() <= 400) return CaloricLevel.DIET;
else if (dish.getCalories() <= 700) return CaloricLevel.NORMAL;
else return CaloricLevel.FAT;
})
)
);
//遍历map
System.out.println(map);
for (Dish.Type key:map.keySet()){
System.out.println(map.get(key));
Map<CaloricLevel, List<Dish>> map2= map.get(key);
for (CaloricLevel key2:map2.keySet()){
System.out.println(map2.get(key2));
}
}
}
//按子组收集数据
@Test
public void test14(){
Map<Dish.Type, Long> map = menu.stream()
.collect(groupingBy(Dish::getType, counting()));
System.out.println(map);
}
//分区
@Test
public void test15(){
Map<Boolean, List<Dish>> map = menu.stream()
.collect(partitioningBy(Dish::isVegetarian));
System.out.println(map);
for (Boolean key:map.keySet()){
System.out.println(map.get(key));
}
}
//多级分区
@Test
public void test16(){
Map<Boolean, Map<Dish.Type, List<Dish>>> map = menu.stream()
.collect(partitioningBy(Dish::isVegetarian, groupingBy(Dish::getType)));
for (Boolean key:map.keySet()){
System.out.println(map.get(key));
}
}
}