散列图以及它如何在幕后工作 [英] Hashmap and how this works behind the scene
问题描述
下面是一个代码示例:
// String key = new String(key);
// String val = new String(value);
String key =key;
String val =value;
HashMap map = new HashMap();
map.put(key,val);
System.out.println(hashmap object created,its key hashcode =+ key.hashCode());
//哈希码是106079
System.out.println(key =+ map.get(key))的hashmap对象值;
//让我们使用具有相同散列码的密钥进行存储
整数intkey = new Integer(106079);
val =value2;
map.put(intkey,val);
System.out.println(hashmap object created。It intkey hashcode =+ intkey.hashCode());
//这将再次返回106079。因此,key和intkey具有相同的散列码
//让我们来获取值
System.out.println(intkey =+ map.get(intkey)的hashmap对象值);
System.out.println(key =+ map.get(key))的hashmap对象值;
返回值与预期一致。我在后面看到,HashMap的工作原理如下:
$ b
- 获取键/值。
- 从密钥中创建一个哈希码。
- 将其存储在同一个存储区中,但是与这是一个LinkedList,我想它将它存储在下一个可用分配中。
- 拿起钥匙,获得散列码,
- 看桶,
- 然后查看LinkedList中第一个元素的键,然后检查key是否传递和key匹配,如果不是则继续下一步,依此类推直到值可以检索。
- 不应该有那个许多桶
- 存储桶与条目不同。一个桶是所有共享相同桶#的条目的逻辑集合(在Java情况下,它是该键的hashCode()函数)。正如其他答案中所述,可以通过多种方式实现存储区溢出,不一定在列表中。
- 还有其他现有的冲突解决方案: http://en.wikipedia.org/wiki/Hash_table#Collision_resolution
HashMap可以通过多种方式实现哈希码冲突解决,不仅可以像您提到的那样利用链接列表
- 它使用Object的equals方法来比较和检索来自同一个存储桶的对象。
Java的HashMap实现不仅使用
LinkedList
策略来处理具有相同键的键值.hashCode()
values。
另外,您可能需要阅读这篇文章
quick question to be sure I understood well how HashMap in java works.
Here is an example of code:
//String key = new String("key"); //String val = new String("value"); String key = "key"; String val = "value"; HashMap map = new HashMap(); map.put(key, val); System.out.println("hashmap object created. Its key hashcode = "+key.hashCode()); // the hashcode is 106079 System.out.println("hashmap object value for key = "+map.get(key)); // Let's store using a key with same hashcode Integer intkey = new Integer(106079); val = "value2"; map.put(intkey, val); System.out.println("hashmap object created. Its intkey hashcode = "+intkey.hashCode()); // this returns 106079 once again. So both key and intkey have the same hashcode // Let's get the values System.out.println("hashmap object value for intkey = "+map.get(intkey)); System.out.println("hashmap object value for key = "+map.get(key));
the returned values are as expected. I read that behind the scene, HashMap works as follows:
- get the key/value.
- make a hashcode from the key.
- store in bucket the key and value objects (in my case bucket number 106079)
same again for the second one.
- stores it within the same bucket but as this is at this point a LinkedList, I suppose store it at "next available allocation".
To get it:
- pick up the key, get hashcode,
- look at the bucket,
- then look at the key from the first element in the LinkedList,
- then check if key passed and key from element match, if not then continue to the next, and so on until value can be retrieved.
Am I understanding this concept correctly?
Many thanks!
EDIT:
many thanks, so to complete: - How does Java HashMap store entries internally - How does a Java HashMap handle different objects with the same hash code?
And:
- There shouldn't be that many "buckets"
- A bucket is not the same as an entry. A bucket is a logical collection of all entries sharing the same bucket# (which in the Java case is a function of the key's hashCode()). As stated in other answers, bucket overflow could be implemented several ways, not necessarily in a List.
- There are other existing collision resolutions: http://en.wikipedia.org/wiki/Hash_table#Collision_resolution
- it uses Object's equals method to compare and retrieve objects from the same bucket.
解决方案Please also note, there are several ways HashMap can implement hash codes collision resolution, not only utilizing linked list as you mentioned
Java's HashMap implementation does not only use
LinkedList
strategy to handle key-values with samekey.hashCode()
values.Also, you may want to read this article
这篇关于散列图以及它如何在幕后工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 存储桶与条目不同。一个桶是所有共享相同桶#的条目的逻辑集合(在Java情况下,它是该键的hashCode()函数)。正如其他答案中所述,可以通过多种方式实现存储区溢出,不一定在列表中。
$ b $
获取它:
我正确理解这个概念吗?非常感谢!
编辑:
非常感谢,所以完成:
- Java HashMap如何在内部存储条目
- Java HashMap如何处理具有相同哈希代码的不同对象?
和: