有没有办法检查两个集合是否包含相同的元素,独立于顺序? [英] Is there a way to check if two Collections contain the same elements, independent of order?
问题描述
我一直在寻找一个操作像 Arrays.equals(a1,a2)
,但忽略元素顺序的方法。我无法在Google Collections中找到它(类似 Iterables.elementsEqual()
,但它确实是排序)和JUnit( assertEquals()
显然只是调用集合上的 equals()
,这取决于集合实现,这不是我想要的)
这将是最好的,如果这样的方法将 Iterable
s,但我也很好,简单地采取集合
s
这样的方法当然会考虑集合中的任何重复元素(因此不能简单地测试 containsAll()
)。 p>
注意,我不是在问如何实现这样的事情,我只是想知道是否有任何标准的集合库有它。
Apache commons-collections有 CollectionUtils#isEqualCollection :
< blockquote>
如果给定的Collections包含完全相同的基数的相同元素,则返回true。
也就是说,如果e的基数
我认为,这是完全相同的你以后的。
I've been looking for a method that operates like Arrays.equals(a1, a2)
, but ignoring the element order. 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()
).
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.
Apache commons-collections has CollectionUtils#isEqualCollection:
Returns true if the given Collections contain exactly the same elements with exactly the same cardinality.
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.
Which is, I think, exactly what you're after.
这篇关于有没有办法检查两个集合是否包含相同的元素,独立于顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!