Java computation set intersection union set

  java

Order

Here is a brief introduction to how to use apache commons and guava’s class libraries to calculate the intersection union of sets.

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);