Java8 中的 Map 接口新增了一批由 default 声明的默认方法以下示例中列举了

  • getOrDefault(Object, V)
  • putIfAbsent(K, V)
  • remove(Object, Object)
  • replace(K, V)
  • replace(K, V, V)
  • replaceAll(BiFunction<? super K, ? super V, ? extends V>)
  • forEach(BiConsumer<? super K, ? super V>)
  • compute(K, BiFunction<? super K, ? super V, ? extends V>)
  • Map.Entry.comparingByValue() / Map.Entry.comparingByKey()
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
private static Map<String, Object> map = new HashMap<>();
private static Map<String, Integer> unsorted = new HashMap<>();
static {
map.put("Paris", "巴黎");
map.put("London", "伦敦");
map.put("NewYork", "纽约");
unsorted.put("a", 2);
unsorted.put("b", 1);
unsorted.put("c", 3);
}
public static void main(String[] args) {
// Java8 以前, 根据 Key 获取值, 若没有匹配的项, 则返回 null
Object hk1 = map.get("HongKong"); // null
// 根据 Key 获取值, 若没有匹配的项, 则返回 defaultValue
Object hk2 = map.getOrDefault("HongKong", "香港"); // 香港
// 遍历
map.forEach((k, v) -> System.out.println(k + " = " + v)); // London = 伦敦
// NewYork = 纽约
// Paris = 巴黎
// 当 key 匹配的项不存在或为 null 时, 则执行 put 并返回 null
Object result1 = map.putIfAbsent("HongKong", "香港"); // null
// 当 key 匹配的项存在且不为 null 时, 则直接返回该项的值
Object result2 = map.putIfAbsent("HongKong", "香干"); // 香港
// 若 key 或 value 其中的一个不匹配时, 则直接返回 false
boolean bool1 = map.remove("HongKong", "香干"); // false
// 当 key 和 value 同时匹配时, 则执行 remove 并返回 true
boolean bool2 = map.remove("HongKong", "香港"); // true
// 当 key 匹配的项不存在时, 则直接返回 null
Object value1 = map.replace("HongKong", "香港"); // null
// 当 key 匹配的项存在时, 用 value 替换之前的值, 并返回替换之前的值
Object value2 = map.replace("NewYork", "纽约!纽约"); // 纽约
// 与新增的 remove 方法类似, key 和 value 同时匹配时才替换
boolean bool3 = map.replace("Paris", "巴黎", "巴黎!巴黎");
// 全部替换
map.replaceAll((k, v) -> k + ", " + v);
// 输出替换后的结果
map.forEach((k, v) -> System.out.println(k + " = " + v)); // London = London, 伦敦
// NewYork = NewYork, 纽约!纽约
// Paris = Paris, 巴黎!巴黎
// 简而言之, replace 规则 : 存在则替换
// 只要 BiFunction 函数产生 non-null 值, 皆执行 put 操作, 并返回该结果
Object obj1 = map.compute("HongKong", (k, v) -> k); // HongKong
// 只要 BiFunction 函数产生 null 值, 如果 map 中存在该 key 匹配的项, 执行 remove 移除, 并返回 null
Object obj2 = map.compute("HongKong", (k, v) -> null); // null
// 未排序之前
unsorted.forEach((k, v) -> System.out.println(k + " = " + v)); // a = 2
// b = 1
// c = 3
// 用于存储排序的结果
Map<String, Integer> sorted = new LinkedHashMap<>();
// 按值自然排序
unsorted.entrySet().stream().sorted(Map.Entry.comparingByValue()).forEach((e) -> sorted.put(e.getKey(), e.getValue()));
// 按值自然排序的结果
sorted.forEach((k, v) -> System.out.println(k + " = " + v)); // b = 1
// a = 2
// c = 3
sorted.clear();
// 按值降序排序
unsorted.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()).forEach((e) -> sorted.put(e.getKey(), e.getValue()));
// 按值降序排序的结果
sorted.forEach((k, v) -> System.out.println(k + " = " + v)); // c = 3
// a = 2
// b = 1
}