我正在嘗試使用執(zhí)行以下操作的 Java 流編寫此代碼 -獲取 KeyValuePairs 列表和 ListOfValues,并<Value, boolean>根據(jù) ListOfKeyValuePairs 中是否存在值填充值來自 ListOfValues 的 Map,布爾值設(shè)置為 true 或 false。我正在開始類似的事情:keyValuePairs.stream().map(KeyValuePair::getValue) ...但無法完成它:(例子:List<KeyValuePairs> has {(1, A1), (2,A2), (3,A3)}
ListofValues has {A1,A3}最終結(jié)果:帶有值的映射:A1,真和 A3,真
1 回答

慕容708150
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
List<KeyValuePairs>
可以轉(zhuǎn)換為 aSet<Value>
以加速進(jìn)一步的查找:
var set = pairs.stream() .map(KeyValuePair::getValue) .collect(Collectors.toSet());
然后,如果生成的地圖不應(yīng)該包含在 中未找到的元素,請pairs
通過set::contains
以下方式將它們過濾掉:
var map = list.stream() .filter(set::contains) .collect(Collectors.toMap(Function.identity(), i -> true));
如果生成的地圖應(yīng)包含來自 的每個(gè)元素list
,無論它們是否已被找到:
var map = list.stream() .collect(Collectors.toMap(Function.identity(), set::contains));
添加回答
舉報(bào)
0/150
提交
取消