java 具有相同哈希码但不相等的两个实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12707869/
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
Two instances having the same hashcode but not equal
提问by auser
I was reading the paragraph quoted below from an article entitled- Java theory and practice: Hashing it out - Defining hashCode() and equals() effectively and correctly
我正在阅读下面从题为Java 理论与实践的文章中引用的段落:将其散列 - 有效且正确地定义 hashCode() 和 equals()
Defining equalityThe Object class has two methods for making inferences about an object's identity: equals() and hashCode(). In general, if you override one of these methods, you must override both, as there are important relationships between them that must be maintained. In particular, if two objects are equal according to the equals() method, they must have the same hashCode() value (although the reverse is not generally true).[emphasis added by me]
定义相等Object 类有两种方法来推断对象的身份:equals() 和 hashCode()。通常,如果您覆盖这些方法之一,则必须覆盖这两个方法,因为它们之间存在必须维护的重要关系。特别是,如果两个对象根据equals()方法相等,那么它们必须有相同的hashCode()值(虽然反过来一般不成立)。 [重点是我加的]
My question relates to the latter bit of the paragraph "although the reverse is not generally true". How is it possible for two different instances of a class to have the same hashCode but not be equal?
我的问题与该段的后一点有关,“尽管相反的情况通常并非如此”。一个类的两个不同实例如何可能具有相同的 hashCode 但不相等?
回答by Zohaib
In simple terms hashcode () is a function to generate hash by some formula, so there can be some collisions, two different values can turn out to have same hashcode.
简单来说hashcode()是一个通过某种公式生成hash的函数,所以可能会有一些冲突,两个不同的值可能会产生相同的hashcode。
If I simply calculate the hashcode by taking mod by 6, then two different values might be having same hashcode.
如果我只是通过将 mod 乘以 6 来计算哈希码,那么两个不同的值可能具有相同的哈希码。
回答by Chetan Gowda
Think of hashcode as something that just reduces the effort in checking equality. If two objects are equal they will definitely have the same hashcode. However if two objects have the same hashcode, they might have a mathematically high similarity but still not be the same. Just for mindset: Think of comparing a duck to an elephant in a zoo. They are highly dissimilar and will have different abstract hashcode, so you dont have to bother comparing their legs, wings etc to check if they are same. However if you are comparing a duck and a swan, they are highly similar and have same abstract hashcode, so now you are down to comparing very minute features of each animal to check for equality. As you reduce the extremeness between two elements being compared, the abstract hashcode becomes more and more concrete. Like comparing ducks and swans has more concrete hashcode than comparing ducks and elephants, comparing different breed of ducks makes the hash code even more concrete, comparing dna of two ducks of same breed makes the hashcode even more concrete. This answer is just designed to create a mindset to understand concept of hashcode. After reading this, you must blur out the understanding of the word hashcode in context of this answer.
将哈希码视为可以减少检查相等性的工作的东西。如果两个对象相等,它们肯定会有相同的哈希码。但是,如果两个对象具有相同的哈希码,则它们可能在数学上具有很高的相似性,但仍然不相同。只是为了心态:想想在动物园中将鸭子与大象进行比较。它们高度不同,并且具有不同的抽象哈希码,因此您不必费心比较它们的腿、翅膀等来检查它们是否相同。但是,如果您要比较一只鸭子和一只天鹅,它们非常相似并且具有相同的抽象哈希码,因此现在您只能比较每种动物的非常微小的特征以检查是否相等。当你减少被比较的两个元素之间的极端性时,抽象的哈希码变得越来越具体。就像比较鸭子和天鹅比比较鸭子和大象有更具体的哈希码,比较不同品种的鸭子会使哈希码更加具体,比较两只同品种鸭子的 dna 使哈希码更加具体。这个答案只是为了创造一种理解哈希码概念的心态。阅读本文后,您必须模糊对本答案上下文中哈希码一词的理解。
回答by Rohit Jain
You can consider hashes to be a bucket
..
你可以考虑hashes to be a bucket
..
- If two objectsare equal, they will go into the same bucket(have same hashcodes)
- But, if the two objectsgo into the same bucket(have same hashcode), that doesn't mean that they must be equal
- Also note that, if two objects are not equal, even then they can have the same hash code.. Obviously, this infers from the above two points..
- 如果两个对象相等,它们将进入同一个桶(具有相同的哈希码)
- 但是,如果两个对象进入同一个桶(具有相同的哈希码),这并不意味着它们必须相等
- 还要注意的是,如果两个对象不相等,即使它们可以具有相同的哈希码..显然,这从以上两点推断..
So, hashcode is nothing but the hash-value for that Bucket.. Any number of objects can have same hashcode, depending upon the algorithm used to calculate the hashcodes..
所以,hashcode 只不过是那个 Bucket 的 hash-value.. 任何数量的对象都可以有相同的 hashcode,这取决于用于计算 hashcodes 的算法。
An ideal algorithm is the one, which generates different hashcodes for different objects. So, there is ideally 1 object
per bucket
.. Of course this is the perfect case, which might not be possible..
一种理想的算法是为不同的对象生成不同的哈希码。所以,理想情况下,1 object
每个bucket
.. 当然这是完美的情况,这可能是不可能的..
A bucket may of course contain several objects, based on some property..
基于某些属性,一个存储桶当然可能包含多个对象。
回答by Tudor
I think the reverse is actually
我认为实际上是相反的
if two objects are NOT equal according to the equals() method, they must have the A DIFFERENT hashCode() value
如果根据 equals() 方法两个对象不相等,则它们必须具有不同的 hashCode() 值
which clearly does not hold since generating unique hashes in the general case is not possible because you're usually trying to map a set of values onto a set of hash codes of lower cardinality.
这显然不成立,因为在一般情况下生成唯一的哈希是不可能的,因为您通常试图将一组值映射到一组基数较低的哈希码上。
回答by AlexR
I will explain it using example. Let's say that hashCode()
of string is based on the string length. In this case the hash code of "foo"
and "bar"
are equal. But "foo"
itself is not equal to "bar"
.
我会用例子来解释它。假设 hashCode()
字符串是基于字符串长度的。在这种情况下,"foo"
和的哈希码"bar"
是相等的。但"foo"
本身不等于"bar"
。
It is because has code implements a kind of formula: you can determine has code for each object but cannot restore object from hash code. There can be several objects with same hash code.
这是因为拥有代码实现了一种公式:您可以确定每个对象的拥有代码,但无法从哈希码中恢复对象。可以有多个具有相同哈希码的对象。
回答by Matt
You can define your hashCode()
implementation to always return 1
fore example. This is perfectly valid: Different instances (which are not equal
) can have the same hashCode
. But the runtime performance of looking up these objects in HashMaps
, Sets
or other types of collections will be very poor (because they all land in the same bucket internally - the lookup performance degrades from O(1)
to O(n)
because you need to traverse the list of objects in the same bucket).
您可以定义您的hashCode()
实现以始终返回1
示例。这是完全有效的:不同的实例(不是equal
)可以具有相同的hashCode
. 但查找这些对象中运行时的性能HashMaps
,Sets
或其他类型的集合会非常差(因为他们在同一个桶所有土地内部-查找性能下降的O(1)
到O(n)
,因为你需要遍历在同一个桶对象的列表)。
Also consider taking a look at how HashMaps work in Java.
还可以考虑看看HashMaps 在 Java 中是如何工作的。
回答by Peter Ilfrich
A hash code of an object is usually much smaller than the original object. This is one purpose of the hash function. So you can imagine, that if you have n different objects (say all permutations of a class) it is not possible to code them in m (where m < n) different and smaller (than the original object) unique codes.
对象的哈希码通常比原始对象小得多。这是散列函数的目的之一。所以你可以想象,如果你有 n 个不同的对象(比如一个类的所有排列),就不可能用 m(其中 m < n)不同且更小(比原始对象)的唯一代码对它们进行编码。
回答by Ali Motamedi
Let me show with an example:
让我举个例子:
suppose that the HashCode of a string obtains as follow: hashCode = sum of each character ASCII code (but we know, real hash is more complicated)
假设一个字符串的HashCode得到如下: hashCode = 每个字符ASCII码的总和(但我们知道,真正的hash更复杂)
For example : hash code of "abc" calculate in such form : 49+50+51 = 150
例如:“abc”的哈希码以这样的形式计算:49+50+51 = 150
Then hash code of "acb" equals : 49+51+50 = 150
那么“acb”的哈希码等于:49+51+50 = 150
And so on. as you can see, there are many strings having hashcode=150 but they are not equal.
等等。如您所见,有许多字符串的 hashcode=150 但它们不相等。