【发布时间】:2015-07-14 09:05:33
【问题描述】:
获得两组交集的简单方法是什么? 我有:
Set<Long> set1 = {1,2,3}
Set<Long> set2 = {2,3,4}
我看起来或方法如下:
Set<Long> intersection = new HashSet<>();
intersection.intersect(set1, set2);
而intersection.toString() 产生我的集合包含{2,3}
【问题讨论】:
-
使用
set1.retainAll(set2) -
如果你不介意使用 apache commons。你在这里:commons.apache.org/proper/commons-collections/javadocs/…, java.util.Collection) util 方法称为intersection