-
Notifications
You must be signed in to change notification settings - Fork 3
/
MapMethodsTest.java
146 lines (135 loc) · 4.63 KB
/
MapMethodsTest.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
package other;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.junit.Test;
import java.util.*;
/**
* @author liuqian
* @date 2019/10/7 19:11.
*/
public class MapMethodsTest {
@Test
public void mapMergeTest() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
List<StudentScore> studentScoreList = buildATestList();
// 按照学生分组,求得每个学生的总分
// 常规做法
Map<String, Integer> studentScoreMap = new HashMap<>();
studentScoreList.forEach(studentScore -> {
if (studentScoreMap.containsKey(studentScore.getStuName())) {
studentScoreMap.put(studentScore.getStuName(), studentScoreMap.get(studentScore.getStuName()) + studentScore.getScore());
} else {
studentScoreMap.put(studentScore.getStuName(), studentScore.getScore());
}
});
// {"李四":228,"张三":215,"王五":235}
System.out.println(objectMapper.writeValueAsString(studentScoreMap));
// merge() 方法
Map<String, Integer> studentScoreMap2 = new HashMap<>();
studentScoreList.forEach(studentScore -> studentScoreMap2.merge(
studentScore.getStuName(),
studentScore.getScore(),
Integer::sum));
// {"李四":228,"张三":215,"王五":235}
System.out.println(objectMapper.writeValueAsString(studentScoreMap2));
}
@Test
public void mapComputeTest() {
String k = "key";
Map<String, Integer> map = new HashMap<String, Integer>() {{
put(k, 1);
}};
// 2
System.out.println(map.compute(k, (key, oldVal) -> oldVal + 1));
}
@Test
public void mapCountComputeTest() {
List<String> words = new ArrayList<String>() {{
add("A");
add("B");
add("C");
add("A");
add("C");
add("E");
add("E");
add("E");
add("E");
add("A");
add("E");
}};
// the word appear times
Map<String, Integer> wordCountMap = new HashMap<>();
words.forEach(word -> wordCountMap.compute(word, (key, oldCount) -> {
if (Objects.isNull(oldCount)) {
return 1;
}
return oldCount + 1;
}));
System.out.println(wordCountMap);
}
private List<StudentScore> buildATestList() {
List<StudentScore> studentScoreList = new ArrayList<>();
StudentScore studentScore1 = new StudentScore() {{
setStuName("张三");
setSubject("语文");
setScore(70);
}};
StudentScore studentScore2 = new StudentScore() {{
setStuName("张三");
setSubject("数学");
setScore(80);
}};
StudentScore studentScore3 = new StudentScore() {{
setStuName("张三");
setSubject("英语");
setScore(65);
}};
StudentScore studentScore4 = new StudentScore() {{
setStuName("李四");
setSubject("语文");
setScore(68);
}};
StudentScore studentScore5 = new StudentScore() {{
setStuName("李四");
setSubject("数学");
setScore(70);
}};
StudentScore studentScore6 = new StudentScore() {{
setStuName("李四");
setSubject("英语");
setScore(90);
}};
StudentScore studentScore7 = new StudentScore() {{
setStuName("王五");
setSubject("语文");
setScore(80);
}};
StudentScore studentScore8 = new StudentScore() {{
setStuName("王五");
setSubject("数学");
setScore(85);
}};
StudentScore studentScore9 = new StudentScore() {{
setStuName("王五");
setSubject("英语");
setScore(70);
}};
studentScoreList.add(studentScore1);
studentScoreList.add(studentScore2);
studentScoreList.add(studentScore3);
studentScoreList.add(studentScore4);
studentScoreList.add(studentScore5);
studentScoreList.add(studentScore6);
studentScoreList.add(studentScore7);
studentScoreList.add(studentScore8);
studentScoreList.add(studentScore9);
return studentScoreList;
}
@Data
private class StudentScore {
private String stuName;
private String subject;
private Integer score;
}
}