Skip to content

Commit a11a967

Browse files
committed
Stream接口&Function
1 parent 9f01f74 commit a11a967

File tree

11 files changed

+1013
-1062
lines changed

11 files changed

+1013
-1062
lines changed

Java8基础.md

Lines changed: 494 additions & 0 deletions
Large diffs are not rendered by default.

Java8实践.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Java8-进阶
2+
3+
4+
- Map的双重循环
5+
6+
```
7+
//对map的entry对象来做stream操作,使用两次forEach
8+
Map<String, Long> map = new HashMap<>();
9+
crowdMap.entrySet().stream()
10+
.map(Map.Entry::getValue)
11+
.forEach(x -> x.entrySet().forEach(y -> {
12+
if (map.containsKey(y.getKey()))
13+
map.put(y.getKey(), map.get(y.getKey()) + y.getValue());
14+
else map.put(y.getKey(), y.getValue());
15+
}));
16+
17+
//对map的entry对象来做stream操作,使用flatMap将stream合并
18+
Map<String, Long> map = new HashMap<>();
19+
crowdMap.entrySet().stream()
20+
.map(Map.Entry::getValue)
21+
.flatMap(x -> x.entrySet().stream())
22+
.forEach(y -> {
23+
if (map.containsKey(y.getKey()))
24+
map.put(y.getKey(), map.get(y.getKey()) + y.getValue());
25+
else map.put(y.getKey(), y.getValue());
26+
});
27+
28+
//使用map本身的foreach语句
29+
Map<String, Long> map = new HashMap<>();
30+
crowdMap.forEach((key, value) -> value.forEach((x, y) -> {
31+
if (map.containsKey(x))
32+
map.put(x, map.get(x) + y);
33+
map.put(x, y);
34+
}));
35+
```
36+
37+
- List的多次分组
38+
39+
```
40+
//使用groupingBy将ApproveRuleDetail对象分别按照item和detail分组,并计次
41+
Map<String, Map<String, Long>> detailMap = approveRuleDetailList.stream()
42+
.collect(Collectors
43+
.groupingBy(ApproveRuleDetail::getItem, Collectors.
44+
groupingBy(ApproveRuleDetail::getDetail, Collectors.counting())));
45+
```
46+
47+
- List按照自定义条件分组
48+
49+
```
50+
//使用自定义的Function函数,将年龄按照每10岁分组
51+
Function<Integer, Integer> ageGroup = x -> x / 10;
52+
Map<Integer, List<StatisticsPipeline>> ageMap = statisticsPipelineList
53+
.stream()
54+
.collect(Collectors.groupingBy(y -> ageGroup.apply(y.getAge())));
55+
```
56+
57+
```
58+
//将年龄按不同方式分组
59+
Function<Integer, Integer> ageCredit = x -> {
60+
if (x <= 18)
61+
return 18;
62+
else if (x >= 40)
63+
return 40;
64+
else return x;
65+
};
66+
67+
//将StatisticsPipeline转化为suggestion
68+
ToDoubleFunction<StatisticsPipeline> mapper = StatisticsPipeline::getSuggestion;
69+
70+
//将人群按照不同年龄分组,并计算每个年龄段的suggestion的平均值
71+
Map<Integer, Double> ageCreditMap = statisticsPipelineList
72+
.stream()
73+
.collect(Collectors.groupingBy(y -> ageCredit.apply(y.getAge()), Collectors.averagingDouble(mapper)));
74+
```
75+
76+
- 多个数据求集合
77+
78+
```
79+
//合并数据
80+
private BiFunction<Integer[], ApprovePipeline, Integer[]> accumulator = (x, y) -> new Integer[]{
81+
x[0] + y.getAuth(), x[1] + y.getAntiFraud(), x[2] + y.getCreditRule(), x[3] + y.getModelReject(), x[4] + y.getSuggestion()
82+
};
83+
84+
//合并集合
85+
private BinaryOperator<Integer[]> combiner = (x, y) -> new Integer[]{x[0] + y[0], x[1] + y[1], x[2] + y[2], x[3] + y[3], x[4] + y[4]};
86+
87+
//将ApprovePipeline对象的不同数据相加
88+
Integer[] detail = approvePipelineList.stream().reduce(new Integer[]{0, 0, 0, 0, 0}, accumulator, combiner);
89+
```
90+
91+
- 多个数据求集合-多重合并
92+
93+
```
94+
private BiFunction<Integer[], ApprovePipeline, Integer[]> accumulator = (x, y) -> new Integer[]{
95+
x[0] += y.getAuth(), x[1] += y.getAntiFraud(), x[2] += y.getCreditRule(), x[3] += y.getModelReject(), x[4] += y.getSuggestion()
96+
};
97+
//合并数据
98+
BiFunction<Integer[], Map.Entry<String, List<ApprovePipeline>>, Integer[]> newAccumulator = (x, y) -> {
99+
List<ApprovePipeline> pipelineList = y.getValue();
100+
Integer[] data = pipelineList.stream().reduce(new Integer[]{0, 0, 0, 0, 0}, accumulator, combiner);
101+
return new Integer[]{
102+
x[0] += data[0], x[1] += data[1], x[2] += data[2], x[3] += data[3], x[4] += data[4]
103+
};
104+
};
105+
//最终reduce
106+
Integer[] total = channelMap.entrySet().stream().reduce(new Integer[]{0, 0, 0, 0, 0}, newAccumulator, combiner);
107+
```
108+
109+
- map最大多项和
110+
111+
```
112+
Long hourC3 = hourMap.entrySet().stream().mapToLong(Map.Entry::getValue).sorted().limit(3).sum();
113+
```

0 commit comments

Comments
 (0)