Man*_* Zi 1 java lambda java-8 java-stream
是否可以根据其他列表中的条目减少列表?
这是类似的东西:
list1.forEach(l1 -> {
list2.forEach(l2 -> {
if (l1.getId() == l2.getId())
reducedActions.add(l2); // that's the new list where the filtered entries should be add
});
});
Run Code Online (Sandbox Code Playgroud)
编辑:我所感谢的:
l1: 10, 15, ,16
l2: 1, 2, 12, 10, 11, 14, 16
reducedActions: 10, 16
Run Code Online (Sandbox Code Playgroud)
那这个呢?
Set<Integer> ids = list2.stream().map(e -> e.getId()).collect(Collectors.toSet());
List<ElementType> reducedActions = list1.stream().filter(e -> ids.contains(e.getId())).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
466 次 |
| 最近记录: |