JDK1.8之Lambda写法记录

List 转 Set

1
2
3
4
5
List<User> userList = Lists.newArrayList();
Set<String> userNameSet1 = userList.stream().map(User::getUserName).collect(Collectors.toSet());

// 先过滤,再转Set
Set<String> userNameSet2 = userList.stream().filter(u -> u.getAge() >= 20).map(User::getUserName).collect(Collectors.toSet());

List 转 Map

1
2
3
4
5
6
List<User> userList = Lists.newArrayList();
Map<String, List<User>> userMap1 = userList.stream().collect(Collectors.groupingBy(User::getUserId));

Map<String, User> userMap2 = userList.stream().collect(Collectors.toMap(User::getUserId, Function.identity()));

Map<String, User> userMap3 = userList.stream().collect(Collectors.toMap(User::getUserId, User::getUserName));

List 过滤

1
2
3
List<User> userList = Lists.newArrayList();
List<String> schoolIdList = Lists.newArrayList();
List<User> stuUserList = userList.stream().filter(u -> schoolIdList.contains(u.getSchoolId())).collect(Collectors.toList());

根据实体类某个属性去重

1
2
// Collectors.collectingAndThen
List<User> distinctList = userList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new));

Map 取 Key 补集和差集

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
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class NapUtil {
/**
* 取Map集合的差集
*/
public static <S,T> Map<S, T> getDifferenceSetByGuava(Map<S, T> leftMap, Map<S, T> rightMap) {
if (null != leftMap && null != rightMap) {

Set<S> leftMapKey = leftMap.keySet();
Set<S> rightMapKey = rightMap.keySet();
Set<S> differenceSet = Sets.difference(leftMapKey, rightMapKey);
Map<S, T> result = Maps.newHashMap();
for (S key : differenceSet) {
result.put(key, leftMap.get(key));
}
return result;

} else {
return null;
}
}

/**
* 取Map集合的并集
*/
public static <S,T> Map<S, T> getUnionSetByGuava(Map<S, T> leftMap, Map<S, T> rightMap) {
if (null != leftMap && null != rightMap) {

Set<S> leftMapKey = leftMap.keySet();
Set<S> rightMapKey = rightMap.keySet();
Set<S> differenceSet = Sets.union(leftMapKey, rightMapKey);
Map<S, T> result = Maps.newHashMap();
for (S key : differenceSet) {
if (leftMap.containsKey(key)) {
result.put(key, leftMap.get(key));
} else {
result.put(key, rightMap.get(key));
}
}
return result;

} else {
return null;
}
}

/**
* 取Map集合的交集(String,String)
*/
public static <S,T> Map<S, T> getIntersectionSetByGuava(Map<S, T> leftMap, Map<S, T> rightMap) {
if (null != leftMap && null != rightMap) {

Set<S> leftMapKey = leftMap.keySet();
Set<S> rightMapKey = rightMap.keySet();
Set<S> differenceSet = Sets.intersection(leftMapKey, rightMapKey);
Map<S, T> result = Maps.newHashMap();
for (S key : differenceSet) {
result.put(key, leftMap.get(key));
}
return result;

} else {
return null;
}
}


public static void main(String[] args) {
Map<String, Person> map1 = new HashMap<>();
map1.put("a", new Person(1));
map1.put("b", new Person(2));
map1.put("c", new Person(3));

Map<String, Person> map2 = new HashMap<>();
map2.put("c", new Person(3));
map2.put("d", new Person(4));
map2.put("e", new Person(5));


Map<String, Person> diffMap1 = getDifferenceSetByGuava(map1, map2);
System.out.println("-------------差集结果,入参:A,B 出参:A-B后A中剩余的 -----------");
diffMap1.forEach((k, v) -> System.out.println(k + ":" + v));

Map<String, Person> diffMap2 = getDifferenceSetByGuava(map2, map1);
System.out.println("-------------差集结果,入参:B,A 出参:B-A后B中剩余的 -----------");
diffMap2.forEach((k, v) -> System.out.println(k + ":" + v));

Map<String, Person> unionMap = getUnionSetByGuava(map1, map2);
System.out.println("-------------并集结果-----------");
unionMap.forEach((k, v) -> System.out.println(k + ":" + v));

Map<String, Person> intersectionMap = getIntersectionSetByGuava(map1, map2);
System.out.println("-------------交结果-----------");
intersectionMap.forEach((k, v) -> System.out.println(k + ":" + v));
}
}





  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2024 Liangxj
  • 访问人数: | 浏览次数: