Skip to content

Commit e8529cc

Browse files
committed
Map相关操作
1 parent e85e3c9 commit e8529cc

File tree

2 files changed

+102
-7
lines changed

2 files changed

+102
-7
lines changed

JdkLearn/src/main/java/com/learnjava/lambda/LambdaComparatorDemo.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public class LambdaComparatorDemo {
2121
static {
2222
Employee e1 = new Employee(1,23,"M","Rick","Beethovan", "2021-04-01");
2323
Employee e2 = new Employee(2,13,"F","Martina","Hengis", "2021-04-02");
24-
Employee e3 = new Employee(3,43,"M","Ricky","Martin","2021-04-03" );
25-
Employee e4 = new Employee(4,26,"M","Jon","Lowman", "2021-04-04");
26-
Employee e5 = new Employee(5,19,"F","Cristine","Maria", "2021-04-05");
24+
Employee e3 = new Employee(3,43,"M","Ricky","Martin","2021-04-09" );
25+
Employee e4 = new Employee(4,26,"M","Jon","Lowman", "2021-04-10");
26+
Employee e5 = new Employee(5,19,"F","Cristine","Maria", "2021-04-01");
2727
Employee e6 = new Employee(6,15,"M","David","Feezor", "2021-04-06");
28-
Employee e7 = new Employee(7,68,"F","Melissa","Roy", "2021-04-07");
28+
Employee e7 = new Employee(7,68,"F","Melissa","Roy", "2021-04-06");
2929
Employee e8 = new Employee(8,79,"M","Alex","Gussin", "2021-04-08");
3030
Employee e9 = new Employee(9,15,"F","Neetu","Singh", "2021-04-09");
3131
Employee e10 = new Employee(10,45,"M","Naveen","Jain", "2021-04-10");

JdkLearn/src/main/java/com/learnjava/lambda/LambdaMapDemo.java

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.learnjava.lambda;
22

33
import java.text.SimpleDateFormat;
4-
import java.util.Date;
5-
import java.util.List;
4+
import java.util.*;
5+
import java.util.function.Function;
66
import java.util.stream.Collectors;
77

88
/**
@@ -14,7 +14,10 @@ public class LambdaMapDemo {
1414
public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
1515

1616
public static void main(String[] args) {
17-
test01();
17+
// test01();
18+
// test02();
19+
// test03();
20+
test04();
1821
}
1922

2023
/**
@@ -23,6 +26,7 @@ public static void main(String[] args) {
2326
public static void test01() {
2427
List<LambdaComparatorDemo.Employee> sortedByDate = LambdaComparatorDemo.employees
2528
.stream()
29+
.sorted()
2630
.sorted((a, b) -> {
2731
int result;
2832
try {
@@ -36,6 +40,97 @@ public static void test01() {
3640
System.out.println(sortedByDate);
3741
}
3842

43+
/**
44+
* HashMap的merge方法,如果key相同,则通过merge来对key相同的袁术进行处理
45+
*/
46+
public static void test02() {
47+
String key = "money";
48+
Map<String, Integer> map = new HashMap<String, Integer>(){{put(key, 100);}};
49+
50+
// 第三个参数时BiFunction,聚合函数(可以这么理解)
51+
map.merge(key,100,(oldValue, newValue) -> oldValue + newValue);
52+
// map.merge(key, 100,Integer::sum);
53+
System.out.println(map);
54+
}
55+
56+
/**
57+
* 对map进行排序
58+
*/
59+
public static void test03() {
60+
Map<String, Integer> codes = new HashMap<>();
61+
codes.put("2021-04-01", 1);
62+
codes.put("2021-04-15", 49);
63+
codes.put("2021-04-10", 33);
64+
codes.put("2021-04-05", 86);
65+
codes.put("2021-04-20", 92);
66+
67+
// 先将Map转化为List,通过collect处理后再转为Map
68+
Map<String, Integer> sortedMap = codes.entrySet()
69+
.stream()
70+
// .sorted((c1, c2) -> c2.getValue().compareTo(c1.getValue()))
71+
// .sorted(Map.Entry.comparingByValue())
72+
.sorted((c1, c2) -> c2.getKey().compareTo(c1.getKey()))
73+
.collect(
74+
Collectors.toMap(
75+
Map.Entry::getKey,
76+
Map.Entry::getValue,
77+
(oldVal, newVal) -> oldVal,
78+
LinkedHashMap::new
79+
)
80+
);
81+
82+
sortedMap.entrySet().forEach(System.out::println);
83+
84+
}
85+
86+
/**
87+
* 将list转为map,并用其中一个value作为key
88+
*/
89+
public static void test04() {
90+
LinkedHashMap<String, LambdaComparatorDemo.Employee> collect = LambdaComparatorDemo.employees
91+
.stream()
92+
.collect(
93+
Collectors.toMap(
94+
LambdaComparatorDemo.Employee::getDate,
95+
// 这样是返回本身对象的一个表达式,还可以用Function.identity()
96+
// Function.indentity() 就是 t -> t
97+
// employee -> employee,
98+
Function.identity(),
99+
(oldVal, newVal) -> {
100+
// 重复的key就将年纪相加,然后FirstName通过--->加起来
101+
oldVal.setAge(oldVal.getAge() + newVal.getAge());
102+
oldVal.setFirstName(oldVal
103+
.getFirstName()
104+
.concat("--->")
105+
.concat(newVal.getFirstName()));
106+
return oldVal;
107+
},
108+
LinkedHashMap::new
109+
)
110+
);
111+
// 这样打印出的map元素不好观察
112+
// System.out.println(collect);
113+
// collect.entrySet().forEach(System.out::println);
114+
115+
LinkedHashMap<String, LambdaComparatorDemo.Employee> sortedCollect = collect
116+
.entrySet()
117+
.stream()
118+
.sorted((a, b) -> b.getKey().compareTo(a.getKey()))
119+
.collect(
120+
Collectors.toMap(
121+
Map.Entry::getKey,
122+
Map.Entry::getValue,
123+
// 上面已经对重复key做处理了,这里就直接范围oldVal就行
124+
(oldVal, newVal) -> oldVal,
125+
LinkedHashMap::new
126+
)
127+
);
128+
129+
// 根据日期排序
130+
sortedCollect.entrySet()
131+
.forEach(System.out::println);
132+
}
133+
39134
public static Date getDate(String date) throws Exception {
40135
return format.parse(date);
41136
}

0 commit comments

Comments
 (0)