Java HashMap如何处理具有相同哈希代码的不同对象? [英] How does a Java HashMap handle different objects with the same hash code?

查看:135
本文介绍了Java HashMap如何处理具有相同哈希代码的不同对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解,我认为:


  1. 两个对象拥有相同的哈希码是完全合法的。

  2. 如果两个对象相等(使用equals()方法),那么它们具有相同的散列码。
  3. 如果两个对象不相等,则它们不能具有相同的散列码

我是否正确?

现在如果没错,以下问题:
HashMap 内部使用对象的哈希码。因此,如果两个对象可以具有相同的哈希码,那么 HashMap 如何跟踪它使用哪个键?



可以有人解释了 HashMap 在内部是如何使用对象的哈希码的?

解决方案

一个HashMap可以像这样工作(这有点简单,但它说明了基本机制):

它有许多桶,它用来存储键值对。每个存储桶都有唯一的编号 - 这就是标识存储桶的标识。当您将键值对放入映射中时,散列映射将查看键的哈希码,并将该对存储在标识符为键的哈希码的桶中。例如:密钥的哈希码是235 - >该对存储在桶号235中。(请注意,一个桶可以存储多于一个键值对)。



当你在hashmap中查找一个值时,通过给它一个键值,它会首先查看你给出的键值的哈希码。然后,hashmap将查看相应的存储桶,然后通过将它与 equals()进行比较,将您提供的密钥与存储桶中所有对的密钥进行比较。 。

现在你可以看到这对于查找映射中的键值对非常有效:通过密钥的哈希码,哈希映射立即知道哪个桶看看,这样它只需要测试什么在桶里。



看看上面的机制,你也可以看到 hashCode() equals()键的方法:


  • 如果两个键相同( equals()返回 true 他们),他们的 hashCode()方法必须返回相同的数字。如果密钥违反了这个规则,那么相同的密钥可能会存储在不同的桶中,并且hashmap将无法找到键值对(因为它将在同一个桶中查找)。


  • 如果两个密钥不同,那么它们的哈希码是否相同并不重要。如果它们的哈希码相同,它们将被存储在同一个桶中,在这种情况下,哈希映射将使用 equals()来区分它们。



As per my understanding I think:

  1. It is perfectly legal for two objects to have the same hashcode.
  2. If two objects are equal (using the equals() method) then they have the same hashcode.
  3. If two objects are not equal then they cannot have the same hashcode

Am I correct?

Now if am correct, I have the following question: The HashMap internally uses the hashcode of the object. So if two objects can have the same hashcode, then how can the HashMap track which key it uses?

Can someone explain how the HashMap internally uses the hashcode of the object?

解决方案

A hashmap works like this (this is a little bit simplified, but it illustrates the basic mechanism):

It has a number of "buckets" which it uses to store key-value pairs in. Each bucket has a unique number - that's what identifies the bucket. When you put a key-value pair into the map, the hashmap will look at the hash code of the key, and store the pair in the bucket of which the identifier is the hash code of the key. For example: The hash code of the key is 235 -> the pair is stored in bucket number 235. (Note that one bucket can store more then one key-value pair).

When you lookup a value in the hashmap, by giving it a key, it will first look at the hash code of the key that you gave. The hashmap will then look into the corresponding bucket, and then it will compare the key that you gave with the keys of all pairs in the bucket, by comparing them with equals().

Now you can see how this is very efficient for looking up key-value pairs in a map: by the hash code of the key the hashmap immediately knows in which bucket to look, so that it only has to test against what's in that bucket.

Looking at the above mechanism, you can also see what requirements are necessary on the hashCode() and equals() methods of keys:

  • If two keys are the same (equals() returns true when you compare them), their hashCode() method must return the same number. If keys violate this, then keys that are equal might be stored in different buckets, and the hashmap would not be able to find key-value pairs (because it's going to look in the same bucket).

  • If two keys are different, then it doesn't matter if their hash codes are the same or not. They will be stored in the same bucket if their hash codes are the same, and in this case, the hashmap will use equals() to tell them apart.

这篇关于Java HashMap如何处理具有相同哈希代码的不同对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆