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

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

问题描述

根据我的理解,我认为:

As per my understanding I think:

  1. 两个对象具有相同的哈希码是完全合法的.
  2. 如果两个对象相等(使用 equals() 方法),则它们具有相同的哈希码.
  3. 如果两个对象不相等,则它们不能具有相同的哈希码

我说得对吗?

现在如果我是对的,我有以下问题:HashMap 在内部使用对象的哈希码.那么如果两个对象可以有相同的哈希码,那么 HashMap 如何跟踪它使用的键?

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?

谁能解释一下HashMap内部是如何使用对象的hashcode的?

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

推荐答案

hashmap 的工作原理是这样的(这有点简化,但它说明了基本机制):

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

它有许多用于存储键值对的存储桶".每个存储桶都有一个唯一的编号——这就是标识存储桶的原因.当您将键值对放入映射中时,哈希映射会查看键的哈希码,并将该对存储在标识符为键的哈希码的桶中.例如:key的hash码为235->一对存储在桶号235中.(注意一个桶可以存储多个key-value对)

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).

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

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.

看上面的机制,也可以看出对key的hashCode()equals()方法有什么要求:

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

  • 如果两个键相同(比较时equals()返回true),它们的hashCode()方法必须返回相同的数字.如果键违反了这一点,那么相等的键可能会存储在不同的存储桶中,并且哈希图将无法找到键值对(因为它会在同一个存储桶中查找).

  • 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).

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

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天全站免登陆