欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中Map的computeIfAbsent方法詳解

 更新時間:2023年11月30日 11:11:28   作者:啊幾  
這篇文章主要介紹了Java的Map中computeIfAbsent方法詳解,在jdk1.8中Map接口新增了一個computeIfAbsent方法,這是Map接口中的默認實現(xiàn)該方法是首先判斷緩存Map中是否存在指定的key的值,如果不存在,會調(diào)用mappingFunction(key)計算key的value,需要的朋友可以參考下

前言

在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)文章

最新評論