computeIfAbsent 方法
方法原型 V computeIfAbsent(K key, Function<? super <, ? extends V> mappingFunction)
, 与上一个方法相反,如果指定的 key 不存在或相关的 value 为 null 时,设置 key 与关联一个计算出的非 null 值,计算出的值为 null 的话什么也不做(不会去删除相应的 key)。如果 key 存在并且对应 value 为 null 的话什么也不做。同样,方法的返回值也是最终的 map.get(key)。
参考实现:
if (map.get(key) == null) {
V newValue = mappingFunction.apply(key);
if (newValue != null)
map.put(key, newValue);
}
示例及效果:
String ret;
Map<String, String> map = new HashMap<>();
ret = map.computeIfAbsent(“a”, key -> key + “123”); //ret “a123”, map 为 {“a”:“a123”}
ret = map.computeIfAbsent(“a”, key -> key + “456”); //ret “a123”, map 为 {“a”:“a123”}
map.put(“a”, null);
ret = map.computeIfAbsent(“a”, key -> key + “456”); //ret “a456”, map 为 {“a”:“a456”}
ret = map.computeIfAbsent(“a”, key -> null); //ret 为 “a456”, map 为 {“a”:“a456”}
replace(K key, V value) 方法
只要 key 存在,不管对应值是否为 null,则用传入的 value 替代原来的值。即使传入的 value 是 null 也会用来替代原来的值,而不是删除,注意这对于 value 不能为 null 值的 Map 实现将会造成 NullPointerException。key 不存在不会修改 Map 的内容,返回值总是原始的 map.get(key) 值。
参考实现:
if (map.containsKey(key)) {
return map.put(key, value);
} else
return null;
示例及效果:
String ret;
Map<String, String> map = new HashMap<>();
ret = map.replace(“a”, “abc”); //ret 为 null,map 为 {}
map.put(“a”, “ddd”);
ret = map.replace(“a”, “abc”); //ret 为 “ddd”, map 为 {“a”:“abc”}
ret = map.replace(“a”, null); //ret 为 “abc”, map 为 {“a”:null}
ret = map.replace(“a”, “ddd”); //ret 为 null, map 为 {“a”:“ddd”}
replace(K key, V oldValue, V newValue)
当且仅当 key 存在,并且对应值与 oldValue 不相等,才用 newValue 作为 key 的新相关联值,返回值为是否进行了替换。
参考实现:
if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
map.put(key, newValue);
return true;
} else
return false;
示例及效果:
boolean ret;
Map<String, String> map = new HashMap<>() ;
ret = map.replace(“a”, null, “aaa”); //ret 为 false, map 为 {}
map.put(“a”, null);
ret = map.replace(“a”, null, “aaa”); //ret 为 true, map 为 {“a”:“aaa”}
ret = map.replace(“a”, “aaa”, null); //ret 为 true, map 为 {“a”:null}
ret = map.replace(“a”, “aaa”, “bbb”);//ret 为 false, map 为 {“a”:null}
replaceAll 方法
方法原型 void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)
。它更像一个传统函数型语言的 map 函数,即对于 Map 中的每一个元素应用函数 function, 输入为 key 和 value。
参考实现:
for (Map.Entry<K, V> entry : map.entrySet())
entry.setValue(function.apply(entry.getKey(), entry.getValue()));
示例及效果:
Map<String, String> map = new HashMap<>() ;
map.put(“a”, “aaa”);
map.put(“b”, “bbb”); //map 为 {“a”:“aaa”,“b”:“bbb”}
map.replaceAll((key, value) -> key + “-” + value); //map 为 {“a”:“a-aaa”,“b”:“b-bbb”}
remove(key, value)
这个也不用多说,key 与 value 都匹配时才删除。
参考实现:
if (map.containsKey(key) && Objects.equals(map.get(key), value)) {
map.remove(key);
return true;
} else
return false;
compute 方法
方法原型 V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
, 它是 computeIfAbsent
与 computeIfPresent
的结合体。也就是既不管 key 存不存在,也不管 key 对应的值是否为 null, compute
死活都要设置与 key 相关联的值,或者计算出的值为 null 时删除相应的 key, 返回值为最终的 map.get(key)。
参考实现:
V oldValue = map.get(key);
V newValue = remappingFunction.apply(key, oldValue);
if (oldValue != null ) {
if (newValue != null)
map.put(key, newValue);
else
map.remove(key);
} else {
if (newValue != null)
map.put(key, newValue);
else
return null;
}
示例及效果:
String ret;
Map<String, String> map = new HashMap<>() ;
ret = map.compute(“a”, (key, value) -> “a” + value); //ret=“anull”, map={“a”:“anull”}
ret = map.compute(“a”, (key, value) -> “a” + value); //ret=“aanull”, map={“a”:“aanull”}
ret = map.compute(“a”, (key, value) -> null); //ret=null, map={}
merge 方法
方法原型 V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFucntion)
,这是至今来说比较神秘的一个方法,尚未使用到它。如果指定的 key 不存在,或相应的值为 null 时,则设置 value 为相关联的值。否则根据 key 对应的旧值和 value 计算出新的值 newValue,newValue 为 null 时,删除该key, 否则设置 key 对应的值为 newValue。方法的返回值也是最终的 map.get(key) 值。
参考实现:
V oldValue = map.get(key);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if (newValue == null)
map.remove(key);
else
map.put(key, newValue);
注意 value 不能为 null 值
示例及效果:
String ret;
Map<String, String> map = new HashMap<>() ;
ret = map.merge(“a”, “aa”, (oldValue, value) -> oldValue + “-” + value); //ret=“aa”, map={“a”:“aa”}
ret = map.merge(“a”, “bb”, (oldValue, value) -> oldValue + “-” + value); //ret=“aa-bb”, map={“a”:“aa-bb”}
ret = map.merge(“a”, “bb”, (oldValue, value) -> null); //ret=null, map={}
map.put(“a”, null);
ret = map.merge(“a”, “aa”, (oldValue, value) -> oldValue + “-” + value); //ret=“aa”, map={“a”:“aa”}
map.put(“a”, null);
ret = map.merge(“a”, “bb”, (oldValue, value) -> null); //ret=“bb”, map={“a”:“bb”}
ret = map.merge(“a”, null, (oldValue, value) -> oldValue + “-” + value); //NullPointerException, value 不能为 null
Map.Entry comparingByKey 和 comparingByValue 方法
另外介绍一下 Map.Entry 新加的两个排序方法,它们分别有无参与带 Comparator 参数可嵌套使用的两个版本。comparingByKey()
, comparingByKey(Comparator<? super K> cmp)
, comparingByValue()
和 comparingByValue(Comparator<? super V> cmp)
。
最后
![](https://upload-images.jianshu.io/upload_images/13465705-
494767c3c363eb03.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
由于篇幅原因,就不多做展示了
mg-AZA8UW3k-1723369717594)]
![](https://upload-images.jianshu.io/upload_images/13465705-
494767c3c363eb03.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
[外链图片转存中…(img-NlrqAm7z-1723369717595)]
[外链图片转存中…(img-CDb5mmAr-1723369717595)]
由于篇幅原因,就不多做展示了