简体   繁体   English

有没有办法检查两个集合是否包含相同的元素,与订单无关?

[英]Is there a way to check if two Collections contain the same elements, independent of order?

I've been looking for a method that operates like Arrays.equals(a1, a2) , but ignoring the element order. 我一直在寻找一个像Arrays.equals(a1, a2)一样操作的方法,但忽略了元素顺序。 I haven't been able to find it in either Google Collections (something like Iterables.elementsEqual() , but that does account for ordering) and JUnit ( assertEquals() obviously just calls equals() on the Collection, which depends on the Collection implementation, and that's not what I want) It would be best if such a method would take Iterable s, but I'm also fine with simply taking Collection s Such a method would of course take into account any duplicate elements in the collection (so it can't simply test for containsAll() ). 我无法在任何Google Collections中找到它(类似于Iterables.elementsEqual() ,但确实考虑了排序)和JUnit( assertEquals()显然只是在Collection上调用equals() ,这取决于Collection实现,这不是我想要的)最好是这样的方法会采用Iterable ,但我也可以简单地使用Collection s这样的方法当然会考虑Collection的任何重复元素(所以它不能简单地测试containsAll() )。

Note that I'm not asking how to implement such a thing, I was just wondering if any of the standard Collections libraries have it. 请注意,我不是在问如何实现这样的事情,我只是想知道是否有任何标准的Collections库都有它。

Apache commons-collections has CollectionUtils#isEqualCollection : Apache commons-collections有CollectionUtils#isEqualCollection

Returns true if the given Collections contain exactly the same elements with exactly the same cardinality. 如果给定的集合包含具有完全相同基数的完全相同的元素,则返回true。

That is, if the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b. 也就是说,如果a中的e的基数等于b中的e的基数,则对于a或b中的每个元素e。

Which is, I think, exactly what you're after. 我认为,这正是你所追求的。

This is three method calls and uses Google Collections Guava , but is possibly as simple as it gets: 这是三个方法调用并使用Google Collections Guava ,但可能很简单:

HashMultiset.create(c1).equals(HashMultiset.create(c2));

Creating the temporary Multiset s may appear wasteful, but to compare the collections efficiently you need to index them somehow. 创建临时Multiset可能看起来很浪费,但要有效地比较集合,您需要以某种方式索引它们。

如果你想忽略秩序,那么如何测试集合是否相等?

new HashSet(c1).equals(new HashSet(c2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查两个集合是否包含相同元素的方式,与订单无关? - Way to check if two Collections contain the same elements, independent of order? 检查两个字符串是否包含相同顺序的相同字符 - Check whether two strings contain same characters in same order 使用自定义相等函数检查两个集合是否相同(忽略顺序) - Check if two collections are the same (ignoring order) using a custom equality function 如何检查两个对象是否包含相同的单词,而不管顺序如何? - How to check if two objects contain the same words regardless of order? 查找两个不同列表是否包含完全相同元素的简单方法? - Simple way to find if two different lists contain exactly the same elements? 检查两个集合是否包含至少一个相同元素的快速方法 - fast way to check if two sets contain at least one same element 检查两个数组是否具有相同顺序的元素 - Check whether two arrays have same elements in some order 如何 JUnit 测试这两个 List<E> 以相同的顺序包含相同的元素? - How to JUnit test that two List<E> contain the same elements in the same order? 使用Java检查两个html标记是否包含不同顺序的相同属性 - Check if two html tags contain same attributes in different order using Java Junit:如何检查 2 collections 是否与不同顺序的元素相等? - Junit: How to check if 2 collections are equals with elements in different order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM