实体交集差集
java
List<OcApplySquareVo> jiaoList = list.stream()
.filter(item ->//此处加!得到差集,不加得到交集
!list2.stream().map(OcApplySquareVo::getApplySubName)
.collect(Collectors.toList())
.contains(item.getApplySubName())
).collect(Collectors.toList());
获取两个List集合之间的交集、并集、差集、补集
java
@Test
public void intersection() {
List<Integer> intersection = Lists.newArrayList();
for (Integer e1 : list1) {
for (Integer e2 : list2) {
if (e1.equals(e2)) {
intersection.add(e1);
}
}
}
System.out.println("intersection <手动遍历> 交集结果是: " + intersection);
intersection = list1.stream()
.filter(list2::contains)
.collect(Collectors.toList());
System.out.println("intersection <Stream流> 交集结果是: " + intersection);
list1.retainAll(list2);
System.out.println("intersection <retainAll> 方法 交集结果是: " + list1);
Collection collection = CollectionUtils.intersection(list1, list2);
System.out.println("intersection <CollectionUtils.intersection> 方法 交集结果是:" + collection);
}
资料: