java8中compute和merge方法如何使用

java8中compute和merge方法如何使用,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比盈江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式盈江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋盈江地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。

基礎(chǔ)數(shù)據(jù):

KeyAndValue a1 = new KeyAndValue().setName("kevin").setValue("lee");
KeyAndValue a5 = new KeyAndValue().setName("kevin2").setValue("lee5");
KeyAndValue a6 = new KeyAndValue().setName("kevin3").setValue("lee8");

場景一對應(yīng)的方法是compute方法
compute,
key存在的元素進(jìn)行加工,并返回加工后的元素,同時(shí)map中該元素也被替換成加工后的。如果對value進(jìn)行計(jì)算或邏輯處理,必須保證value非null。示例如下:

Map<String, KeyAndValue> maps = new HashMap<>();
maps.put("a",a1);
System.out.println(maps);
{a=KeyAndValue(name=kevin, value=lee)}//key存在時(shí)KeyAndValue compute1 = maps.compute("a", (k, v) -> {
    Optional.ofNullable(v).ifPresent(v1 -> v1.setValue(v.getValue().concat("__ddddd")));return v;
});
System.out.println(compute1);// KeyAndValue(name=kevin, value=lee__ddddd)System.out.println(maps);// {a=KeyAndValue(name=kevin, value=lee__ddddd)}// key不存在時(shí)KeyAndValue compute = maps.compute("b", (k, v) -> {
    Optional.ofNullable(v).ifPresent(v1 -> v1.setValue(v.getValue().concat("__ddddd")));return v;
});
System.out.println(compute);//nullSystem.out.println(maps);// {a=KeyAndValue(name=kevin, value=lee__ddddd)}

當(dāng)然針對value不存在和存在,也有針對性的方法(computeIfAbsent和computeIfPresent)。

computeIfAbsent
key不存在或null,則處理,返回處理后結(jié)果,并更新value;

//key不存在或nullMap<String, KeyAndValue> map = new HashMap<>();
System.out.println(map);//{}KeyAndValue computeIfAbsent = map.computeIfAbsent("a", s -> {return a6;
});
System.out.println(computeIfAbsent);// KeyAndValue(name=kevin3, value=lee8)System.out.println(map);// {a=KeyAndValue(name=kevin3, value=lee8)}


key存在,則不進(jìn)行處理,返回value,不更新map。示例如下:

// key存在。不進(jìn)行任何處理,返回valueMap<String, KeyAndValue> map = new HashMap<>();
map.put("a",a1);
System.out.println(map);// {a=KeyAndValue(name=kevin, value=lee)}KeyAndValue computeIfAbsent1 = map.computeIfAbsent("a", s -> {return a6;
});
System.out.println(computeIfAbsent1);// KeyAndValue(name=kevin, value=lee)System.out.println(map);// {a=KeyAndValue(name=kevin, value=lee)}

computeIfPresent,
key存在則處理,返回處理后結(jié)果,并更新value;

//key存在則處理Map<String, KeyAndValue> map = new HashMap<>();
map.put("a",a1);
System.out.println(map);// {a=KeyAndValue(name=kevin, value=lee)}KeyAndValue computeIfPresent = map.computeIfPresent("a", (k, v) -> {return v.setValue(v.getValue().concat("_00000"));
});
System.out.println(computeIfPresent);// KeyAndValue(name=kevin, value=lee_00000)System.out.println(map);// {a=KeyAndValue(name=kevin, value=lee_00000)}


key不存在或null,則不進(jìn)行處理,返回null,不更新value

//key不存在則處理Map<String, KeyAndValue> map = new HashMap<>();// map.put("a",null);System.out.println(map);//{}KeyAndValue computeIfPresent = map.computeIfPresent("a", (k, v) -> {return v.setValue(v.getValue().concat("_00000"));
});
System.out.println(computeIfPresent);// nullSystem.out.println(map);// {}

場景二對應(yīng)的方法merge

merge:key存在的元素進(jìn)行合并,并返回加工后的元素
a6
就是合進(jìn)來的新值,a6newV是同一對象,所以newVa6可互換;
oldVmap中原有key對應(yīng)的值,oldV有值則合并,無值則新增對應(yīng)的kvalue=newV的鍵值對

Map<String, KeyAndValue> map = new HashMap<>();
map.put("a",a1);
System.out.println(map);// {a=KeyAndValue(name=kevin, value=lee)}
//“a”有值,則合并KeyAndValue merge = map.merge("a", a6, (oldV, newV) -> {
    oldV.setValue(newV.getValue()+"___"+oldV.getValue());return oldV;

});
System.out.println(merge);// KeyAndValue(name=kevin, value=lee8___lee)System.out.println(map);// {a=KeyAndValue(name=kevin, value=lee8___lee)}//“b”無值,則新增"b":newVKeyAndValue merge1 = map.merge("b", a5, (oldV, newV) -> {
    oldV.setValue(oldV.getValue()+"___"+oldV.getValue());return oldV;    //a5就是合進(jìn)來的新值,a5和newV是同一對象,所以newV和a5可互換;    // oldV是map中原有key=“b”對應(yīng)的值,oldV有值則合并,無值則新增});
System.out.println(merge1);// KeyAndValue(name=kevin2, value=lee5)System.out.println(map);// {a=KeyAndValue(name=kevin, value=lee), b=KeyAndValue(name=kevin2, value=lee5)}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)頁名稱:java8中compute和merge方法如何使用
網(wǎng)頁URL:http://muchs.cn/article6/ghesog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站排名靜態(tài)網(wǎng)站、網(wǎng)站收錄建站公司、企業(yè)建站

廣告

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

成都定制網(wǎng)站建設(shè)