java 无论顺序如何,Hashset 上的 .equals 都返回 true 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14555486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does .equals on Hashset return true regardless of order?
提问by user1054740
For the Hashset in java there is a .equals method comparing elements in each set. Will this return true regardless of order?
对于 java 中的 Hashset,有一个 .equals 方法比较每个集合中的元素。无论顺序如何,这都会返回 true 吗?
Example, let's say we have one set with elements {a,b,c} and another with elements {b,c,a}
例如,假设我们有一个包含元素 {a,b,c} 的集合,另一个包含元素 {b,c,a}
if you use .equals on these two sets will it return true, or does it have to be sorted?
如果在这两个集合上使用 .equals 会返回 true,还是必须对其进行排序?
回答by Deepak Borania
This should return true. The documentation says :
这应该返回true。文档说:
Compares the specified object with this set for equality. Returns true if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that the equals method works properly across different implementations of the Set interface.
比较指定的对象与此集合是否相等。如果给定的对象也是一个集合,两个集合的大小相同,并且给定集合的每个成员都包含在这个集合中,则返回 true。这确保了 equals 方法在 Set 接口的不同实现中正常工作。
回答by Matt Ball
Java HashSet
s are unordered– they do not have an ordering. Therefore your question simply cannot be asked as posed. The set {a,b,c}
is the same as the set {b,c,a}
. That said, HashSet
inherit's AbstractSet#equals(Object)
which tells us the following:
JavaHashSet
是无序的——它们没有 ordering。因此,您的问题根本不能按提出的方式提出。集合{a,b,c}
与集合相同{b,c,a}
。也就是说,HashSet
继承AbstractSet#equals(Object)
告诉我们以下内容:
Compares the specified object with this set for equality. Returns
true
if the given object is also a set, the two sets have the same size, and every member of the given set is contained in this set. This ensures that theequals
method works properly across different implementations of theSet
interface.
比较指定的对象与此集合是否相等。返回
true
如果给定的对象也是一个集合,两个集合的大小相同,并且给定集合的每个成员都包含在这个集合中。这确保该equals
方法在Set
接口的不同实现中正常工作。
回答by Hui Zheng
HashSet
implements the Set
interface(modeling the mathematical set abstraction), which contains no order information and therefore sort makes no sense in a set. As a result, equals
in a Set
only considers the members and disregards the orders of members.
HashSet
实现Set
接口(建模数学集合抽象),它不包含顺序信息,因此排序在集合中没有意义。结果,equals
in aSet
只考虑成员,不考虑成员的命令。
回答by Jasper Blues
Yes, it will be true because a Set has no implicit order.
是的,这是真的,因为 Set 没有隐式顺序。
You can apply order to a set by using adding a Comparator, or using a special case of Set, such as TreeSet.
您可以通过添加 Comparator 或使用 Set 的特殊情况(例如 TreeSet)对集合应用顺序。
Incidentally, a hashcode is used for fast comparison in a Set. According to the equals contract, any two objects that are considered "equal" must have the same hashcode.
顺便提一下,哈希码用于在 Set 中进行快速比较。根据 equals 契约,任何两个被认为“相等”的对象必须具有相同的哈希码。
This means a Set only has to fall-back to more time consuming methods in the unlikely event of a hashcode colission.
这意味着 Set 只需要在不太可能发生的哈希码冲突的情况下回退到更耗时的方法。
回答by Dmitry
Hash is essentially an unordered list, so yes,
哈希本质上是一个无序列表,所以是的,
if hash A contains {1,2,3,4,5}, and hash B contains {3,1,5,4,2}, then they will be equal.
如果散列 A 包含 {1,2,3,4,5},散列 B 包含 {3,1,5,4,2},则它们相等。