如果我有两个集合:

Set<Integer> test1 = new HashSet<Integer>();
test1.add(1);
test1.add(2);
test1.add(3);

Set<Integer> test2 = new HashSet<Integer>();
test2.add(1);
test2.add(2);
test2.add(3);
test2.add(4);
test2.add(5);

是否有一种方法来比较它们,只返回一组4和5 ?


当前回答

添加一个解决方案,我最近用过,没有看到这里提到。如果你有Apache Commons Collections可用,那么你可以使用SetUtils#difference方法:

// Returns all the elements of test2 which are not in test1
SetUtils.difference(test2, test1) 

注意,根据文档,返回的集合是一个不可修改的视图:

返回一个不可修改的视图,其中包含给定集合的差值,用a \ b(或a - b)表示。 返回的视图包含a中不属于b的所有元素。

完整文档:https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/SetUtils.html#difference-java.util.Set-java.util.Set-

其他回答

如果你使用Guava(前谷歌集合)库有一个解决方案:

SetView<Number> difference = com.google.common.collect.Sets.difference(test2, test1);

返回的SetView是一个Set,它是一个动态表示,你可以使它不可变或复制到另一个Set。Test1和test2保持不变。

如果你正在使用Java 8,你可以尝试这样做:

public Set<Number> difference(final Set<Number> set1, final Set<Number> set2){
    final Set<Number> larger = set1.size() > set2.size() ? set1 : set2;
    final Set<Number> smaller = larger.equals(set1) ? set2 : set1;
    return larger.stream().filter(n -> !smaller.contains(n)).collect(Collectors.toSet());
}

顺序很重要,因为你需要4和5是int(不是Integer), 使用test2.removeAll (test1)。

添加一个解决方案,我最近用过,没有看到这里提到。如果你有Apache Commons Collections可用,那么你可以使用SetUtils#difference方法:

// Returns all the elements of test2 which are not in test1
SetUtils.difference(test2, test1) 

注意,根据文档,返回的集合是一个不可修改的视图:

返回一个不可修改的视图,其中包含给定集合的差值,用a \ b(或a - b)表示。 返回的视图包含a中不属于b的所有元素。

完整文档:https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/SetUtils.html#difference-java.util.Set-java.util.Set-

您可以使用. addall()创建两个集合的联合,使用. retainall()创建交集,并使用. removeif()从联合中删除交集(或复制的元素)。

HashSet union = new HashSet(group1);
union.addAll(group2);
        
System.out.println("Union: " + union);
        
HashSet intersection = new HashSet(group1);
intersection.retainAll(group2);
        
System.out.println("Intersection: " + intersection);
        
HashSet difference = new HashSet(union);
difference.removeIf(n -> (difference.contains(intersection)));
        
System.out.println("Difference: " + difference);