Map.computeIfPresent() 和Map.computeIfAbsent()方法的使用
一.computeIfPresent() 简介
computeIfPresent() 方法对 hashMap 中指定 key 的值进行重新计算,前提是该 key 存在于 hashMap 中。
computeIfPresent() 方法的语法为:
hashmap.computeIfPresent(K key, BiFunction remappingFunction)
注意:hashmap 是 HashMap 类的一个对象。
参数说明:
- key - 键
- remappingFunction - 重新映射函数,用于重新计算值
返回值
如果 key 对应的 value 不存在,则返回该 null,如果存在,则返回通过 remappingFunction 重新计算后的值。
二.computeIfPresent() 源码
default V computeIfPresent(