Java 打印出不在两个哈希集中的元素
我不想输入全部代码,我觉得这是不必要的。我的问题是:我有两条被“;”分开的大字符串并添加到两个哈希集。这样,我想检查两个集合的元素是否相同,如果不相同,则打印出不同的元素。我不想通过任何迭代来实现它,因为对应的元素是相同的,所以不需要迭代每个元素Java 打印出不在两个哈希集中的元素,java,set,hashset,Java,Set,Hashset,我不想输入全部代码,我觉得这是不必要的。我的问题是:我有两条被“;”分开的大字符串并添加到两个哈希集。这样,我想检查两个集合的元素是否相同,如果不相同,则打印出不同的元素。我不想通过任何迭代来实现它,因为对应的元素是相同的,所以不需要迭代每个元素 Set latestStableRevConfigurationSet = new HashSet(Arrays.asList(latestStableRevConfiguration.split(";"))); Set current
Set latestStableRevConfigurationSet = new HashSet(Arrays.asList(latestStableRevConfiguration.split(";")));
Set currentRevConfigurationSet = new HashSet(Arrays.asList(currentRevConfiguration.split(";")));
assertTrue(latestStableRevConfigurationSet.containsAll(currentRevConfigurationSet));
assertTrue(currentRevConfigurationSet.containsAll(latestStableRevConfigurationSet));
上面的方法我只能断言集合是否“相同”,但如何实现a-B/B-a以便打印出不同的元素?您可以使用番石榴:
SetView<Number> difference = com.google.common.collect.Sets.symmetricDifference(set2, set1);
如果您不想添加新的依赖项,可以稍微修改repo上可用的代码。您可以使用番石榴:
SetView<Number> difference = com.google.common.collect.Sets.symmetricDifference(set2, set1);
如果您不想添加新的依赖项,可以稍微修改repo上的可用代码。您想要的是仅在一个集合中的所有元素。一种方法是将所有元素的并集与
Set union = new HashSet();
union.addAll(latestStableRevConfigurationSet);
union.addAll(currentRevConfigurationSet);
然后取交点,即公共元素
Set intersection = new HashSet();
intersection.addAll(latestStableRevConfigurationSet);
intersection.retainAll(currentRevConfigurationSet);
最后减去两个:
union.removeAll(intersection);
您需要仅在一个集合中的所有元素。一种方法是将所有元素的并集与
Set union = new HashSet();
union.addAll(latestStableRevConfigurationSet);
union.addAll(currentRevConfigurationSet);
然后取交点,即公共元素
Set intersection = new HashSet();
intersection.addAll(latestStableRevConfigurationSet);
intersection.retainAll(currentRevConfigurationSet);
最后减去两个:
union.removeAll(intersection);
试试这个:
Set<String> latestStableRevConfigurationCopy = new HashSet<>(latestStableRevConfigurationSet);
Set currentRevConfigurationCopy = currentRevConfigurationSet;
latestStableRevConfigurationCopy.removeAll(currentRevConfigurationSet );
currentRevConfigurationCopy.removeAll(latestStableRevConfigurationSet );
//this would print all the different elements from both the sets.
System.out.println(latestStableRevConfigurationCopy );
System.out.println(currentRevConfigurationCopy );
试试这个:
Set<String> latestStableRevConfigurationCopy = new HashSet<>(latestStableRevConfigurationSet);
Set currentRevConfigurationCopy = currentRevConfigurationSet;
latestStableRevConfigurationCopy.removeAll(currentRevConfigurationSet );
currentRevConfigurationCopy.removeAll(latestStableRevConfigurationSet );
//this would print all the different elements from both the sets.
System.out.println(latestStableRevConfigurationCopy );
System.out.println(currentRevConfigurationCopy );
这是不必要的复杂。不要再发明轮子了!这就是为什么我高估了你的答案!我完全同意使用一个已经实现了这一点的库是更好的,但是看看如何在不引入新的依赖关系的情况下实现这一点仍然是有用的。这是不必要的复杂。不要再发明轮子了!这就是为什么我高估了你的答案!我完全同意使用一个已经这样做了的库更好,但看看如何在不引入新依赖项的情况下实现这一点仍然是有用的。CurrentRevConfigurationCopy不是currentRevConfigurationSet的副本:它只是对同一集合的另一个引用!是的,我知道。为了便于理解,我将其命名为currentRevConfigurationCopy。您应该仍然知道,原始currentRevConfigurationSet已被修改,以防再次使用。为了避免副作用,应该像您为最新的StablerEVConfigurationSet所做的那样复制。CurrentRevConfigurationCopy不是currentRevConfigurationSet的副本:它只是对同一集合的另一个引用!是的,我知道。为了便于理解,我将其命名为currentRevConfigurationCopy。您应该仍然知道,原始currentRevConfigurationSet已被修改,以防再次使用。为了避免副作用,您应该像为最新StablerEVConfigurationSet所做的那样进行复制。