JDK1.8中Map的getOrDefault、putIfAbsent、computeIfAbsent、merge方法的区别
一、getOrDefault
1.说明
getOrDefault:仅仅是返回值,如果不存在返回指定的默认值,不修改map的结构
2.源码
default V getOrDefault(Object key, V defaultValue) {
V v;
//如果key对应得value不为空,或者Map包含了key,则取得value得值,否则得到默认值。
return (((v = get(key)) != null) || containsKey(key))
? v
: defaultValue;
}
3.代码示例
public class Demo1 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("name", "lxj");
map.put("age", "24");
map.put("sex", "女");
String name = map.getOrDefault("name", "test");
System.out.println(name);// lxj,map中存在name,获得name对应的value
String address = map.getOrDefault("address", "北京");
System.out.println(address);// 北京,map中不存在address,使用默认值“北京”
}
}
二、putIfAbsent
1.说明
putIfAbsent添加键值对,如果map集合中没有该key对应的值,则直接添加,如果已经存在对应的值,则依旧为原来的值。
与put对比:
put添加键值对,如果map集合中没有该key对应的值,则直接添加,如果已经存在对应的值,则覆盖旧值。
2.源码
default V putIfAbsent(K key, V value) {
V v = get(key);
//如果键为空,则添加
if (v == null) {
v = put(key, value);
}
//否则跳过该值不处理
return v;
}
3.代码示例
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.put(1,"tcc");
map.putIfAbsent(2,"tcc2");
System.out.println(map);
//key为1存在,不会覆盖value值
map.putIfAbsent(1,"tccIf");
System.out.println(map);
//key为1存在,会覆盖value值
map.put(1,"tccPut");
System.out.println(map);
}
输出结果如下:
{1=tcc, 2=tcc2}
{1=tcc, 2=tcc2}
{1=tccPut, 2=tcc2}
三、computeIfAbsent
1.说明
computeIfAbsent与putIfAbsent功能基本相似,区别是:
如果传入的value为null时,computeIfAbsent不会进行put操作而putIfAbsent会进行put。
value的参数不同,computeIfAbsent方法提供了value的计算函数,如果value计算耗时时,可以使用computeIfAbsent节约时间。
2.源码
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v;
//如果key对应得value为空
if ((v = get(key)) == null) {
V newValue;
//如果传入的value不为空则put
if ((newValue = mappingFunction.apply(key)) != null) {
put(key, newValue);
return newValue;
}
}
return v;
}
3.代码示例
public static void main(String[] args) {
HashMap<Integer, String> map = new HashMap<>();
map.computeIfAbsent(1, v -> "tcc");
//key存在,不会覆盖
map.computeIfAbsent(1, v -> "tccIf");
System.out.println(map);
//value为空,不会put
map.computeIfAbsent(2, v -> null);
System.out.println(map);
}
运行结果如下:
{1=tcc}
{1=tcc}
四、merge
1.说明
merge添加键值对,如果key对应的value不存在,直接存储value值,如果value值存在,更新value值,我们可以自定义更新规则。
使用新value覆盖旧的value: (value1, value2) -> value2
保留旧的value:(value1, value2) -> value1
保存新value+旧value,例如:(value1, value2) -> value1+ value2
2.源码
//BiFunction函数式接口,提供apply方法
default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
//经过函数修改后的value
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
//计算出来的value为空,删除这个key
if(newValue == null) {
remove(key);
} else {
//存入修改后的value
put(key, newValue);
}
return newValue;
}
3.代码示例
public static void main(String[] args) {
HashMap<Integer, Integer> map = new HashMap<>();
map.put(1,10);
map.put(1,20);
map.merge(1,50,(v1,v2)->v1+v2);
map.merge(2,100,(v1,v2)->v2);
System.out.println(map);
}
运行结果:
{1=70, 2=100}