先附上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