溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JDK8中如何實現(xiàn)兩個for循環(huán)如果key 一樣就替換value

發(fā)布時間:2021-12-17 13:48:45 來源:億速云 閱讀:234 作者:小新 欄目:大數(shù)據(jù)

這篇文章給大家分享的是有關(guān)JDK8中如何實現(xiàn)兩個for循環(huán)如果key 一樣就替換value的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

先看比較的對象

public class DataMsg {private int type;    private int len;    private List<Integer> datas;
}
// 將list轉(zhuǎn)為Map  比雙層for快   如果他們的type 相等的話 只替換data的值 封裝成一個新的listList<DataMsg> newDatas = dtuData.getDataMsgList();Map<Integer, DataMsg> newData = newDatas.stream().collect(
        Collectors.toMap(w -> w.getType(),                w -> w));
// 把他的type 當(dāng)成唯一的key 
finalListDatas.stream().forEach(h -> {
    DeviceDatas deviceDatas2 = new DeviceDatas();    
    if (null != newData.get(h.getType()) && newData.get(h.getType()).getType() == h.getType()) {
        deviceDatas2.setType(newData.get(h.getType()).getType());        
        deviceDatas2.setDatas(newData.get(h.getType()).getDatas());    
        } 
        else {
        deviceDatas2.setType(h.getType());        
        deviceDatas2.setDatas(h.getDatas());    
        }
        list.add(deviceDatas2);});

感謝各位的閱讀!關(guān)于“JDK8中如何實現(xiàn)兩個for循環(huán)如果key 一樣就替換value”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

jdk
AI