1
1
package com .learnjava .lambda ;
2
2
3
3
import java .text .SimpleDateFormat ;
4
- import java .util .Date ;
5
- import java .util .List ;
4
+ import java .util .* ;
5
+ import java .util .function . Function ;
6
6
import java .util .stream .Collectors ;
7
7
8
8
/**
@@ -14,7 +14,10 @@ public class LambdaMapDemo {
14
14
public static SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd" );
15
15
16
16
public static void main (String [] args ) {
17
- test01 ();
17
+ // test01();
18
+ // test02();
19
+ // test03();
20
+ test04 ();
18
21
}
19
22
20
23
/**
@@ -23,6 +26,7 @@ public static void main(String[] args) {
23
26
public static void test01 () {
24
27
List <LambdaComparatorDemo .Employee > sortedByDate = LambdaComparatorDemo .employees
25
28
.stream ()
29
+ .sorted ()
26
30
.sorted ((a , b ) -> {
27
31
int result ;
28
32
try {
@@ -36,6 +40,97 @@ public static void test01() {
36
40
System .out .println (sortedByDate );
37
41
}
38
42
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
+
39
134
public static Date getDate (String date ) throws Exception {
40
135
return format .parse (date );
41
136
}
0 commit comments