Java中Map的computeIfAbsent方法詳解
前言
在jdk1.8中Map接口新增了一個computeIfAbsent方法,這是Map接口中的默認實現(xiàn)
default V computeIfAbsent(K key,
Function<? super K, ? extends V> mappingFunction) {
Objects.requireNonNull(mappingFunction);
V v;
if ((v = get(key)) == null) {
V newValue;
if ((newValue = mappingFunction.apply(key)) != null) {
put(key, newValue);
return newValue;
}
}
return v;
}該方法是首先判斷緩存Map中是否存在指定的key的值,如果不存在,會調(diào)用mappingFunction(key)計算key的value,然后將key = value 放入緩存Map中。
但如果mappingFunction(key)計算出來的value為null或拋出異常,則不會記錄緩存。
代碼實例
例1
/**
* 如果key對應的value值為null,則在map中放入該key和設(shè)置相應的value
*/
public class Test {
public static void main(String[] args) {
Map<Integer,Integer> map = new HashMap<>();
/**
* 方法1:使用方法引用
*/
map.computeIfAbsent(1,Test::compute);
/**
* 方法2:使用匿名內(nèi)部類
*/
map.computeIfAbsent(1, new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer;
}
});
/**
* 方法3:使用lambda
*/
map.computeIfAbsent(1,key -> key);
System.out.println(map.get(1));
}
static public Integer compute(Integer integer){
return integer;
}
}
例2
/**
* 統(tǒng)計List出現(xiàn)相同字符串的個數(shù)
*/
public class Test {
public static void main(String[] args) {
Map<String,AtomicInteger> map = new HashMap<>();
List<String> list = Arrays.asList(new String[]{"1","2","2","3","3","4","4","4"});
list.forEach(str->map.computeIfAbsent(str,k->new AtomicInteger()).incrementAndGet());
System.out.println(map);
}
}
/**
* {1=1, 2=2, 3=2, 4=3}
*/
這里用了兩次lambda,和下面方法是一樣
/**
* 統(tǒng)計List出現(xiàn)相同字符串的個數(shù)
*/
public class Test {
public static void main(String[] args) {
Map<String,AtomicInteger> map = new HashMap<>();
List<String> list = Arrays.asList(new String[]{"1","2","2","3","3","4","4","4"});
list.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
//尋找map中key對應的value,如果map不存在指定key的值則new 一個AtomicInteger對象并加入緩存map中,如果存在則取到對應的AtomicInteger對象并加一
map.computeIfAbsent(s, new Function<String, AtomicInteger>() {
@Override
public AtomicInteger apply(String s) {
return new AtomicInteger();
}
}).incrementAndGet();
}
});
System.out.println(map);
}
}
/**
* {1=1, 2=2, 3=2, 4=3}
*/
總結(jié)
computeIfAbsent在一些實際開發(fā)場景中,能讓我們代碼看去更加簡潔,代碼質(zhì)量看去也更高。
到此這篇關(guān)于Java中Map的computeIfAbsent方法詳解的文章就介紹到這了,更多相關(guān)Map的computeIfAbsent方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot2 jackson實現(xiàn)動態(tài)返回類字段方式
這篇文章主要介紹了springboot2 jackson實現(xiàn)動態(tài)返回類字段方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
mybatis-plus多表分頁查詢最佳實現(xiàn)方法(非常簡單)
這篇文章主要給大家介紹了關(guān)于mybatis-plus多表分頁查詢最佳實現(xiàn)方法,文中介紹的方法非常簡單,MyBatis-Plus中分頁查詢是比較方便的,這個功能在網(wǎng)站中也是非常常用的,這方面的知識點是必備的知識點,需要的朋友可以參考下2023-08-08
Eclipse創(chuàng)建java程序可執(zhí)行jar包教程
這篇文章主要為大家分享了Eclipse創(chuàng)建java程序可執(zhí)行jar包教程,具有一定的實用性和參考價值,感興趣的小伙伴們可以參考一下2016-05-05
詳解Java Bellman-Ford算法原理及實現(xiàn)
Bellman-Ford算法與Dijkstra算法類似,都是以松弛操作作為基礎(chǔ),Bellman-Ford算法是對所有邊都進行松弛操作,本文將詳解Bellman-Ford算法原理及實現(xiàn),感興趣的可以了解一下2022-07-07
kafka?消息隊列中點對點與發(fā)布訂閱的區(qū)別說明
這篇文章主要介紹了kafka?消息隊列中點對點與發(fā)布訂閱的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05
使用Feign擴展包實現(xiàn)微服務(wù)間文件上傳
這篇文章主要為大家詳細介紹了使用Feign擴展包實現(xiàn)微服務(wù)間文件上傳,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04

