第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使用 Java 8 Streams 將列表中的對(duì)象與地圖中的數(shù)據(jù)與條件進(jìn)行匹配并保存到另一個(gè)地圖

如何使用 Java 8 Streams 將列表中的對(duì)象與地圖中的數(shù)據(jù)與條件進(jìn)行匹配并保存到另一個(gè)地圖

SMILET 2023-08-16 17:41:17
尋找解決方案,如果對(duì)象字段以地圖值開頭并保存到另一個(gè)地圖,如何將列表中的對(duì)象與地圖中的數(shù)據(jù)與條件進(jìn)行匹配我有帶有一些數(shù)據(jù)的地圖Map<String, String> dataMap = new HashMap()    dataMap.put("d1", "DATA1")    dataMap.put("d2", "DATA2")    dataMap.put("d3", "DATA3")和 DataElement 對(duì)象的列表    List<DataElement> elements = new ArrayList()elements.add(new DataElement("TEXT1"))elements.add(new DataElement("TEXT2"))elements.add(new DataElement("DATA1_text1"))elements.add(new DataElement("DATA2_text2"))class DataElement {            public field;        public DataElement(String text){            this.field = text        }        public getField(){            return this.field        }    }我正在嘗試獲取新的 Map,其中鍵是第一個(gè)映射中的值,值是列表中的對(duì)象(字段),條件是如果對(duì)象字段以映射值開頭:結(jié)果應(yīng)該是:[d1=DATA1_text1, d2=DATA2_text2]  我的代碼:    Map<String, String> collect2 = dataMap.entrySet().stream()            .filter({ map -> elements.stream()                                .anyMatch({ el -> el.getField().startsWith(map.getValue()) })})            .collect(Collectors.toMap(KEY, VALUE))
查看完整描述

1 回答

?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

希望我的問題是正確的:


Map<String, String> collect2 = 

    dataMap.entrySet()

          .stream()

          .map(e -> elements.stream()

                            // this will search for the first element of the List matching

                            // the value of the current Entry, if exists

                            .filter(el -> el.getField().startsWith(e.getValue()))

                            .findFirst()

                            // this will create a new Entry having the original key and the

                            // value obtained from the List

                            .map(el -> new SimpleEntry<>(e.getKey(),el.getField()))

                            // if findFirst found nothing, map to a null element

                            .orElse(null))

          .filter(Objects::nonNull) // filter out all the nulls

          .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

您正在處理 input 的條目Map,并僅保留具有與 的元素匹配的值的條目List(通過filter(),盡管有一些語法錯(cuò)誤),但您需要將map輸入條目轉(zhuǎn)換為包含所需新值的新條目。


上面的代碼產(chǎn)生Map


{d1=DATA1_text1, d2=DATA2_text2}

對(duì)于給定的輸入。


查看完整回答
反對(duì) 回復(fù) 2023-08-16
  • 1 回答
  • 0 關(guān)注
  • 134 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)