java计算集合交差并集

这里简单介绍下,如何使用apache commons以及guava的类库来计算集合的交差并集。

maven

        
            com.google.guava
            guava
            22.0
        
        
            org.apache.commons
            commons-collections4
            4.1
        

guava

        //交集
        Set intersectionSet = Sets.intersection(set1, set2);
        //差集
        Set subtractSet = Sets.difference(set1, set2);
        //并集
        Set unionSet = Sets.union(set1, set2);

commons-collection4

        //交集
        Collection intersectionSet = CollectionUtils.intersection(set1, set2);
        //差集
        Collection subtractSet = CollectionUtils.subtract(set1, set2);
        //并集
        Collection unionSet = CollectionUtils.union(set1, set2);

你可能感兴趣的:(java计算集合交差并集)