【发布时间】:2019-10-14 14:31:56
【问题描述】:
我有两个列表。我想创建一个地图,其中 true 用于匹配元素,false 用于 java 8 中的唯一元素。
例如。
输入-
列表 1 = [A,B,C,D]
清单 2 = [B,C,Y,Z]
输出-
地图:
A,假
B,真
C,真
D,假
我的代码:
Map<String,Boolean> map = new HashMap<>();
for(String var1 : list1) {
boolean value;
if (CollectionUtils.isNotEmpty(list2)) {
Optional<String> valueOptional = list2.stream()
.filter(e1 -> e1.equalsIgnoreCase(var1))
.findAny();
value = valueOptional.isPresent();
map.put(var1, value);
}
}
【问题讨论】:
-
我看到你也患有 Hybris。不过,请花点时间阅读the help pages,尤其是名为"What topics can I ask about here?" 和"What types of questions should I avoid asking?" 的部分。也请take the tour 阅读how to ask good questions。最后请阅读此question checklist。
标签: java lambda collections java-8 java-stream