jdk1.8中map的compute,computeIfAbsent,computeIfPresent方法怎么用

這篇文章主要為大家展示了“jdk1.8中map的compute,computeIfAbsent,computeIfPresent方法怎么用”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“jdk1.8中map的compute,computeIfAbsent,computeIfPresent方法怎么用”這篇文章吧。

成都創(chuàng)新互聯(lián)公司專注于澤普企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城網(wǎng)站建設(shè)。澤普網(wǎng)站建設(shè)公司,為澤普等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

1.compute

compute:V compute(K key, BiFunction < ? super K, ? super V, ? extends V> remappingFunction)

compute的方法,指定的key在map中的值進(jìn)行操作 不管存不存在,操作完成后保存到map中

        HashMap<String,Integer> map = new HashMap<>();
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);
        Integer integer = map.compute("3", (k,v) -> v+1 );
        //key不管存在不在都會(huì)執(zhí)行后面的函數(shù),并保存到map中
        Integer integer1 = map.compute("4", (k,v) -> {
            if (v==null)return 0;
            return v+1;
        } );
        System.out.println(integer);
        System.out.println(integer1);
        System.out.println(map.toString());
 
打印結(jié)果
4
0
{1=1, 2=2, 3=4, 4=0}

2.computeIfAbsent

computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)

computeIfAbsent的方法有兩個(gè)參數(shù) 第一個(gè)是所選map的key,第二個(gè)是需要做的操作。這個(gè)方法當(dāng)key值不存在時(shí)才起作用。

當(dāng)key存在返回當(dāng)前value值,不存在執(zhí)行函數(shù)并保存到map中

    HashMap<String,Integer> map = new HashMap<>();
    map.put("1",1);
    map.put("2",2);
    map.put("3",3);
    Integer integer = map.computeIfAbsent("3", key -> new Integer(4));//key存在返回value
    Integer integer1 = map.computeIfAbsent("4", key -> new Integer(4));//key不存在執(zhí)行函數(shù)存入
    System.out.println(integer);
    System.out.println(integer1);
    System.out.println(map.toString());
 
 
打印結(jié)果
3
4
{1=1, 2=2, 3=3, 4=4}

3.computeIfPresent

computeIfPresent:V computeIfPresent(K key, BiFunction < ? super K, ? super V, ? extends V> remappingFunction)

computeIfPresent 的方法,對(duì) 指定的 在map中已經(jīng)存在的key的value進(jìn)行操作。只對(duì)已經(jīng)存在key的進(jìn)行操作,其他不操作

        HashMap<String,Integer> map = new HashMap<>();
        map.put("1",1);
        map.put("2",2);
        map.put("3",3);
        //只對(duì)map中存在的key對(duì)應(yīng)的value進(jìn)行操作
        Integer integer = map.computeIfPresent("3", (k,v) -> v+1 );
        Integer integer1 = map.computeIfPresent("4", (k,v) -> {
            if (v==null)return 0;
            return v+1;
        } );
        System.out.println(integer);
        System.out.println(integer1);
        System.out.println(map.toString());
 
 
打印結(jié)果
4
null
{1=1, 2=2, 3=4}

以上是“jdk1.8中map的compute,computeIfAbsent,computeIfPresent方法怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁標(biāo)題:jdk1.8中map的compute,computeIfAbsent,computeIfPresent方法怎么用
URL標(biāo)題:http://bm7419.com/article20/igcjjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)網(wǎng)站收錄、關(guān)鍵詞優(yōu)化、企業(yè)網(wǎng)站制作企業(yè)建站、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營(yíng)銷型網(wǎng)站建設(shè)