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
|
Map<String, String> map = Maps.newHashMap();
Map<String, Integer> mapA = Maps.newHashMap(Map.of("a", 1, "b", 2, "c", 3));
Map<String, Integer> mapB = Maps.newHashMap(Map.of("b", 20, "c", 3, "d", 4));
MapDifference<String, Integer> mapDifference = Maps.difference(mapA, mapB);
// 是否相等 false
mapDifference.areEqual();
// 差集 {b=(2, 20)}
mapDifference.entriesDiffering();
// 仅在左侧 {a=1}
mapDifference.entriesOnlyOnLeft();
// 仅在右侧 {d=4}
mapDifference.entriesOnlyOnRight();
// 交集 {c=3}
mapDifference.entriesInCommon();
// 使用双括号初始化(不推荐,因为这样会创建匿名内部类)
Map<String, Integer> map3 = new HashMap<>() {{
put("key1", 1);
put("key2", 2);
}};
// multiMap
Multimap<Integer, String> multiMap = ArrayListMultimap.create();
multiMap.put(1, "a");
multiMap.put(1, "a");
multiMap.put(1, "c");
multiMap.get(1); // [a, a, c]
|