一文讲透 map.computeIfAbsent putIfAbsent computeIfPresent区别

map.computeIfAbsent

public V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)

如果 key 对应的 value 不存在,则使用获取 mappingFunction 计算后的值,并保存为该 key 的 value,否则返回 value。

import java.util.HashMap;

HashMap<Integer, Integer> hashmap=new HashMap<Integer, Integer>();
hashmap.computeIfAbsent(10, key->Integer.valueOf(7));

HashMap<Integer, ArrayList<Integer>> hashmap=new HashMap<Integer, ArrayList<Integer>>();
hashmap.computeIfAbsent(10, key->new ArrayList<Integer>()).add(3);

HashMap<String, Integer> prices = new HashMap<>();

prices.put("Shoes", 200);
prices.put("Bag", 300);
prices.put("Pant", 150);


// 计算 Shirt 的值
int shirtPrice = prices.computeIfAbsent("Shirt", key -> 280);

// java8 之前。从 map 中根据 key 获取 value 操作可能会有下面的操作

Object key = map.get("key");
if (key == null) {
    key = new Object();
    map.put("key", key);
}
// java8 lambda
Object key = map.computeIfAbsent("key", k -> new Object());

getOrDefault

default V getOrDefault(Object key, V defaultValue)

如果在 map 中存在 key 值则返回 key 对应的映射中的内容,否则返回指定的内容。

HashMap<Integer, Integer> hashmap=new HashMap<Integer, Integer>();
// 不存在 key 值,取 8,同时 key 赋值 8
int test = hashmap.computeIfAbsent(10, key->Integer.valueOf(8)); 
int test2=hashmap.getOrDefault(11, 8); // 不存在 key 值,取 8,不赋值

Map put putIfAbsent

V put(K key, V value);
V putIfAbsent(K key, V value);

这两种方法都是以 key-value 键值对的形式存在到 map 集合中,那么它们两个有什么区别呢?

1.使用 put 方法添加键值对,如果 map 集合中没有该 key 对应的值,则直接添加,并返回 null,如果已经存在对应的值,则会覆盖旧值,value 为新的值。

2.使用 putIfAbsent 方法添加键值对,如果 map 集合中没有该 key 对应的值,则直接添加,并返回 null,如果已经存在对应的值,则依旧为原来的值。

java.util.Map putIfAbsent computeIfAbsent computeIfPresent compute
putIfAbsent
default V putIfAbsent(K key,V value)

如果给定的 key 不存在(或者 key 对应的 value 为 null),关联给定的 key 和给定的 value,并返回 null;如果存在,返回当前值(不会把 value 放进去);

computeIfAbsent
default V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
map.computeIfAbsent(key, k -> new Value(f(k)));
map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);

如果给定的 key 不存在(或者 key 对应的 value 为 null),就去计算 mappingFunction 的值;

如果 mappingFunction 的值不为 null,就把 key = value 放进去;
如果 mappingFunction 的值为 null,就不会记录该映射关系,返回值为 null;
如果计算 mappingFunction 的值的过程出现异常,再次抛出异常,不记录映射关系,返回 null;
如果存在该 key,并且 key 对应的 value 部位 null,返回 null;(HashMap 返回的是旧 value)

computeIfPresent
default V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)

key 存在并且不为空,计算 remappingFunction 的值 value;

如果 value 不为空,保存指定 key 和 value 的映射关系;
如果 value 为 null,remove(key);
如果计算 value 的过程抛出了异常,computeIfPresent 方法中会再次抛出,key 和其对应的值不会改变

compute

default V compute(K key,BiFunction<? super K,? super V,? extends V> remappingFunction)
map.compute(key, (k, v) -> (v == null) ? msg : v.concat(msg))
(Method merge() is often simpler to use for such purposes.)

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

如果 lambda 表达式的值不为空,不论 key 是否已经存在,建立一种映射关系 key = newValue; 否则,不建立映射并返回 null。

putIfAbsent、computeIfAbsent、computeIfPresent、compute的区别

putIfAbsent 和 computeIfAbsent
都是在 key 不存在的时候才会建立 key 和 value 的映射关系;
putIfAbset 不论传入的 value 是否为空,都会建立映射(并不适合所有子类,例如 HashTable),而computeIfAbsent 方法,当存入value为空时,不做任何操作
当 key 不存在时,返回的都是新的 value(为什么不说新插入的 value),即使 computeIfAbsent 在传入的value 为 null 时,不会新建映射关系,但返回的也是 null;

computeIfPresent 和 computeIfAbsent
这两个方法正好相反,前者是在key存在时,才会用新的value替换oldValue
当传入的key存在,并且传入的value为null时,前者会remove(key),把传入的key对应的映射关系移除;而后者不论何时都不会remove();
前者只有在key存在,并且传入的value不为空的时候,返回值是value,其他情况都是返回null;后者只有在key不存在,并且传入的value不为null的时候才会返回value,其他情况都返回null;

compute
新传入的 value 不为 null 就建立映射关系(也就是说不论 key 是否为 null,具体子类再具体分析)
新传入的 value 为 null 时:key 已存在,且老的对应 value 不为 null,移除改映射关系,返回 null;否则,直接返回 null

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Elivis Hu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值