序
这里简单介绍下,如何使用apache commons以及guava的类库来计算集合的交差并集。
maven
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>22.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
guava
//交集
Set<Integer> intersectionSet = Sets.intersection(set1, set2);
//差集
Set<Integer> subtractSet = Sets.difference(set1, set2);
//并集
Set<Integer> unionSet = Sets.union(set1, set2);
commons-collection4
//交集
Collection<Integer> intersectionSet = CollectionUtils.intersection(set1, set2);
//差集
Collection<Integer> subtractSet = CollectionUtils.subtract(set1, set2);
//并集
Collection<Integer> unionSet = CollectionUtils.union(set1, set2);