computeIfAbsent源码解析以及用法

先附上java8 的官方文档,有空可以多研究一下 https://docs.oracle.com/javase/8/docs/api/

源码解析

源码如下:

    
 //1. default
default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {//第一个参数为Map的key,第二个参数是一个函数接口 //2. requireNonNull Objects.requireNonNull(mappingFunction); V v; // 将第一个参数作为key,获取value,并将其赋给v ,判断是否为null if ((v = get(key)) == null) { //若v 为null,则创建一个新的值newValue V newValue; // 3. mappingFunction.apply if ((newValue = mappingFunction.apply(key)) != null) { put(key, newValue); return newValue; } } return v; }

1.default  就我所理解来说,接口方法如果加上default,则必须实现该方法体,其实有点类似于抽象类。但是要知道Java 使用的是单继承、多实现的机制

避免多继承带来的调用歧义的问题。当接口的子类同时拥有具有相同签名的方法时,就需要考虑一种解决冲突的方案

https://blog.csdn.net/u010003835/article/details/76850242   该文章写的很详细

2.  requireNonNull 源码

public static <T> T requireNonNull(T obj) {
        if (obj == null)
            throw new NullPointerException();
        return obj;
    }

很明显,判空处理,抛空指针异常,以后若对一个对象判空处理可以使用这个方法。

3. mappingFunction.apply()

https://www.aliyun.com/jiaocheng/771274.html

我觉得里面比较有意思的是,第一个参数key作为apply() 的参数

例子:

Map<Long, List<Long>> map = new HashMap<>();
for(A a : lists){
    List<Long> types = map.computeIfAbsent(a.getId(), k -> new ArrayList<>());

    types.add(a.getType());
}

将v = map.get(a.getId())
判断v是否为空,若不为空,return v
若为空, 则使用新值newValue  = mappingFunction.apply(a.getId)

map.put(a.getId, newValue)
return newValue

1. 当第一次循环,map.get(a.getId()) == null, return new ArrayList()。

此时map.put(a.getId, types)// types为new ArrayList

types.add(a.getType()) 执行完,map.get(a.getId) 即为新的types --> types[0] 为a.getType()的值

2.第二次循环,若a.getId 为第一步的id

map.get(a.getId()) != null return map.get(a.getId())//即第一步的types

继续 types.add(a.getType())

3. 第三次循环, 若a.getId 是新的id, 则回到第一步

所以此方法适用于一对多的情况下,得到一个对应,Map<T,List<T>>  

用法

map.computeIfAbsent(key, k -> new Value(f(k)));

map.computeIfAbsent(key, k -> new HashSet<V>()).add(v);


参考文档:
default:   https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html 
      https://blog.csdn.net/u010003835/article/details/76850242
Function: https://www.aliyun.com/jiaocheng/771274.html

computeIfAbsent: https://my.oschina.net/simpleton/blog/1552737


原文地址:https://www.cnblogs.com/inspirationBoom/p/9994932.html