Map新增方法

一、putIfAbsent 方法
方法原型 V putIfAbsent(K key, V value) , 如果 key 不存在或相关联的值为 null, 则设置新的 key/value 值。
示例:

String ret;
Map<String, String> map = new HashMap<>();
ret = map.putIfAbsent("a", "aaa"); //ret 为"aaa", map 为 {"a":"aaa"}
ret = map.putIfAbsent("a", "bbb"); //ret 为 "aaa", map 还是 {"a":"aaa"}

map.put("b", null);
ret = map.putIfAbsent("b", "bbb"); //ret 为 "bbb", map 为 {"a":"aaa","b":"bbb"}

二、computeIfPresent 方法
方法原型 V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction),如果指定的 key 存在并且相关联的 value 不为 null 时,根据旧的 key 和 value 计算 newValue 替换旧值,newValue 为 null 则从 map 中删除该 key; key 不存在或相应的值为 null 时则什么也不做,方法的返回值为最终的 map.get(key)。
示例:

String ret;
Map<String, String> map = new HashMap<>();
ret = map.computeIfPresent("a", (key, value) -> key + value); //ret null, map 为 {}
map.put("a", null); //map 为 ["a":null]
ret = map.computeIfPresent("a", (key, value) -> key + value); //ret null, map 为 {"a":null}
map.put("a", "+aaa");
ret = map.computeIfPresent("a", (key, value) -> key + value); //ret "a+aaa", map 为 {"a":"a+aaa"}
ret = map.computeIfPresent("a", (key, value) -> null); //ret 为 null, map 为 {},计算出的 null 把 key 删除了
计算出的值为 null 时直接删除 key 而不是设置对应 key 的值为 null, 这能照顾到值不能为 nullMap 实现,如 HashtableConcurrentMap

三、replace(K key, V value) 方法
只要 key 存在,不管对应值是否为 null,则用传入的 value 替代原来的值。即使传入的 value 是 null 也会用来替代原来的值,而不是删除,注意这对于 value 不能为 null 值的 Map 实现将会造成 NullPointerException。key 不存在不会修改 Map 的内容,返回值总是原始的 map.get(key) 值。
示例:

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 的新相关联值,返回值为是否进行了替换。
示例:

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} false原因:key、value不对应

五、replaceAll 方法
方法原型 void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)。它更像一个传统函数型语言的 map 函数,即对于 Map 中的每一个元素应用函数 function, 输入为 key 和 value。
示例:

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"}

六、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)。
示例:

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) 值。
示例:

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值