hashmap如何确定正确的密钥而不调用equals()
这是我将用作密钥的person
类的示例。
它覆盖equals()
,以便它始终返回false
和hashcode()
始终返回1
。
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
System.out.println("Equals is called");
return false;
}
@Override
public int hashCode() {
System.out.println("Hashcode is called");
return 1;
}
}
现在,让我们将其用作钥匙,然后随机填充地图。
public class Main {
public static void main(String[] args) {
var person1 = new Person("Mike", 21);
var person2 = new Person("Alex", 32);
var person3 = new Person("Andrew", 45);
Map<Person, String> peopleMap = new HashMap<>();
peopleMap.put(person3, "SomeValue3");
peopleMap.put(person2, "SomeValue1");
peopleMap.put(person1, "SomeValue2");
System.out.println("Getting a person \n \n");
System.out.println(peopleMap.get(person3));
}
}
这是我在控制台中看到的内容:
Hashcode is called
Hashcode is called
Equals is called
Hashcode is called
Equals is called
Equals is called
Getting a person
Hashcode is called
SomeValue3
问题:
如果等于始终是
,它如何确定正确的密钥?false
?如何在不调用
获取对象?equals()
的情况下获得正确的密钥?如果我们移动person3对象(我们要得到的一个对象),以便在中间 -
1
equals()
将用于第一个对象。如果我们移动到3个位置,则是这样:peoplemap.put(person2,“ somevalue1”); peoplemap.put(person1,“ somevalue2”); peoplemap.put(person3,“ somevalue3”);
2
times equals()
将被调用(用于person2
和person1
/code>),但没有我们获得的密钥。
为什么是这样?我一直认为地图不能存储订单,但看起来像这样。
Here is an example of a Person
class that I will use as a key.
It overrides equals()
so that it always returns false
and hashCode()
that always returns 1
.
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object o) {
System.out.println("Equals is called");
return false;
}
@Override
public int hashCode() {
System.out.println("Hashcode is called");
return 1;
}
}
Now let's use it as a key and randomly fill the map.
public class Main {
public static void main(String[] args) {
var person1 = new Person("Mike", 21);
var person2 = new Person("Alex", 32);
var person3 = new Person("Andrew", 45);
Map<Person, String> peopleMap = new HashMap<>();
peopleMap.put(person3, "SomeValue3");
peopleMap.put(person2, "SomeValue1");
peopleMap.put(person1, "SomeValue2");
System.out.println("Getting a person \n \n");
System.out.println(peopleMap.get(person3));
}
}
Here is what I see in a console:
Hashcode is called
Hashcode is called
Equals is called
Hashcode is called
Equals is called
Equals is called
Getting a person
Hashcode is called
SomeValue3
Questions:
How could it determine the right key if the equals is always
false
?How could it get the right key without even calling
equals()
for getting an object by the key?If we move adding the person3 object down ( the one we are getting ) so it is in the middle -
1
equals()
will be called for the first object. If we move adding to the 3 position, like this:peopleMap.put(person2, "SomeValue1"); peopleMap.put(person1, "SomeValue2"); peopleMap.put(person3, "SomeValue3");
2
times equals()
will be called ( for person2
and person1
) but none for the key we are getting.
Why is it so? I always thought that map can't store an order, but look like this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
等于
实施违反了该方法的合同,特别是Javadoc中的第一个条款:但是,
hashmap
实现假定条件所持,并且作为优化,将首先检查密钥对象是否通过参考为是否相同,然后再使用equals
。当您查看hashmap
的源代码时,您可以看到这一点。https://github.com/openjdk/jdk/blob/0f801fe6fd2fcc181121f9846f9846f6869ca3a03e18a/src/src/java.base/java.base/share/share/share/share/classes/classes/java/java/java/java/java/java/hashmap.java-java-java-jjava-jjava-jjava#l57-pha
youtha ihava i>通过调试器执行。
Your
equals
implementation violates the contract of the method, specifically the first clause in the javadoc:However the
HashMap
implementation assumes the condition holds and as an optimization will first check if the key object is the same by reference before usingequals
. You can see this when you look at the source code ofHashMap
.https://github.com/openjdk/jdk/blob/0f801fe6fd2fcc181121f9846f6869ca3a03e18a/src/java.base/share/classes/java/util/HashMap.java#L577-L579
You can also confirm this by stepping through the execution with a debugger.
hashmap
在引擎盖下维护 buckets 的 array 。每个桶对应于一系列哈希。 buckets 中的条目表示为 nodes ,形式链接列表或 red> red-> red-black tree (取决于 bucket 中的节点)。您已经创建了一个具有损坏的
hashcode()
的类,该类别始终会产生相同的哈希,而当您将其用作键时,它们最终将进入 相同的桶 。因此,这些对象基本上会导致 碰撞 在hashmap
中。在使用
person
的实例为键 和调用get()
提供一个提供其中一个之一现有键,此方法将在内部调用getNode()
。getNode()
会找到相应的 bucket (基于提供的 key 的 hash ),如果是此存储桶的内容不是null
- IE包含A list 或A tree ,它将穿越 list ( tree )检查每个 node ,然后首先在调用equals()
()之前,在这种情况下 em>)它将使用键上的身份比较。如果键指向同一对象,则此比较很快,无需使用
equals()
()进行比较()复杂对象图)。这就是为什么即使损坏的
等于/hashcode
hashmap
也能够识别所需的条目,当您给出与键的完全相同的对象时。HashMap
maintains an array of buckets under the hood. Each bucket corresponds to a range of hashes. Entries in the buckets are represented as nodes which form either a Linked list or Red-black tree (depending on the number of nodes in the bucket).You've created a class with a broken
hashCode()
which instances always produce identical hashes and when you use them as keys they will end up in the same bucket. So basically these objects would cause collisions in theHashMap
.After you've populated the
HashMap
using instances ofPerson
as keys and invokingget()
providing one of the existing keys, this method will internally invokegetNode()
.getNode()
would find the corresponding bucket (based on the hash of the provided key), and if the contents of this bucket isn'tnull
- i.e. the bucket contains either a list or a tree, it would traverse through the list (tree) checking each node and firstly before invokingequals()
(which is useless in this case) it would use the identity comparison on keys.And if the keys point to the same object, this comparison is fast and there's no need to resort to comparison using
equals()
(which might be costful in case of the complex object graph).That's why even with broken
equals/hashCode
HashMap
is able to identify the required entry, when you're giving exactly the same object as a key.您可以参考
hashtable内部工作
以获取详细信息,因为很难在此处解释。简而言之。hashtable
。init_capacity = 16
(hashmap)=&gt; 16不同的 bucketid's [0-15],就像一系列参考一样,最初值=null
。每个阵列的每个插槽称为 bin/bucket
bucketid = @overrided hashcode +一些内部计算
您的hashcode()每次都返回 1 Inge> code> bucketid = 1 +(Say)3 = 4 [每次] 。
3. INVOKES hashcode一次 =&gt; bucketid = 4。现在调用2次检查重复。 IE。 person3的1次,person2的第二名。
因此输出:
希望这会有所帮助:)
you can refer
hashTable internal working
for detail information as it is difficult to explain it here. Have tried to explain in short.HashTable
.init_capacity=16
(HashMap) => 16 distinct bucketId's [0-15] , like an array of references, initially value=null
.each slot of array is called as bin/bucket
Java HashTable internally use Singly Linear LinkedList
bucketId calculated internally like,
bucketId = @Overrided HashCode + some internal calculation
your Hashcode() returns everytime 1 implies
bucketId = 1 + (say)3 = 4 [everytime]
.3.invokes Hashcode one time => bucketId = 4. Now invokes equals 2 times to check duplicates . ie. 1time for person3, 2ndly for person2.
hence output:
HOPE THIS HELPS :)