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對應(yīng)的value值為null,則在map中放入該key和設(shè)置相應(yīng)的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對應(yīng)的value,如果map不存在指定key的值則new 一個AtomicInteger對象并加入緩存map中,如果存在則取到對應(yīng)的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)文章
Mybatis-plus使用注解 @TableField(exist = false)
這篇文章主要介紹了Mybatis-plus使用注解 @TableField(exist = false),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2021-03-03簡單談?wù)刯ava中final,finally,finalize的區(qū)別
Java中final、finally、finalize的區(qū)別與用法,困擾了不少學(xué)習者,下面我們就這個問題進行一些探討,希望對大家的學(xué)習有所幫助。2016-05-05springmvc九大組件之HandlerAdapter詳解
這篇文章主要介紹了springmvc九大組件之HandlerAdapter詳解,RequestMappingHandlerAdapter支持的handler的類型是HandlerMethod,而HandlerMethod是通過解析@RequestMapping注解獲得的,需要的朋友可以參考下2023-11-11springboot2整合redis使用lettuce連接池的方法(解決lettuce連接池無效問題)
這篇文章主要介紹了springboot2整合redis使用lettuce連接池(解決lettuce連接池無效問題),本文給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12