本文介绍了如何以及何时在HashMap中完成重新哈希处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对哈希和重新哈希有些困惑.以下是我的理解,如果我错了,请纠正我.

根据图片,bucket实际上是Entry类的数组,它以链接列表的形式存储元素.每个新的键值对,其键具有与条目数组"存储桶相同的哈希码,将被作为条目对象存储在存储该哈希码元素的存储桶中.如果键具有当前不在Entry Array存储桶中的新哈希码,则将添加带有各自哈希码的新存储桶.

现在的问题是,何时真正进行哈希处理?

情况1:假设我的Entry数组具有哈希码1,2 3的存储桶,并且在Entry Array Bucket达到12时添加了新元素,就可以了,但是只要有一个新元素的哈希码为13(假设我在哈希码1的序列中添加元素,然后添加2,依此类推,依次类推),将创建一个新的Map/Entry Bucket数组(请说明是哪个),新容量为32,现在Entry数组可以容纳不同的32个桶.

情况2:桶装物料的数量,HashMap 16的默认容量表示它可以在其中存储16个元素,或装在单个桶内的装料.由于负载因子为.75,因此,一旦添加第13个元素,就会创建一个新的bucket数组,其中包含32个元素,即现在所有链接列表中的Entry节点总数可以是32个.

我认为情况2是正确的.请展开重新哈希"过程,如果使用此图,则更好.

解决方案

重新哈希处理增加了可用存储桶的数量,该数量是当前HashMap中存储的条目数量的函数.

HashMap实现确定应增加存储桶数以保持预期的O(1)查找和插入性能时,就会发生这种情况.

关于.75默认负载因数以及添加第13个条目时如何导致HashMap重塑的信息,您是正确的.

但是,default capacity of HashMap 16 means it can store 16 element in it是不正确的.任何存储桶都可以存储多个条目.但是,为了保持所需的性能,每个存储桶的平均条目数应该很小.这就是我们拥有负载因子的原因,也是我们应该使用适当的hashCode()以便将密钥尽可能均匀地分布在存储桶中的原因.

i have some confusion about Hashing and Rehashing. below is my understanding please correct me if i am wrong.

as per picture, bucket is actually the array of Entry class which stores the element in the form of linked list. each new key-value pair, whose key has same hashcode of Entry Array bucket will be stored as a Entry Object in bucket which is storing that hashcode elements. if the key has new hashcode which is currently not present in the Entry Array bucket then a new bucket will be added with the respective hashcode.

now the question is , when actually rehashing occurs?

case 1: lets suppose i have Entry array having bucket of hashcode 1,2 3 with addition of new element at the time Entry Array Bucket reached 12 its OK , but as soon as a new element comes whose hashcode is 13( assuming i am adding element in series of hashcode 1 then 2 and so on), a new Map / Array of Entry Bucket( please clarify which one) will be created, and the new capacity will be 32, now the array of Entry can hold distinct 32 buckets.

case 2 : number of bucket dosent matter, default capacity of HashMap 16 means it can store 16 element in it , dosent matter in single bucket or anyhow. because of load factor .75, as soon as 13th element is added a new array of bucket will be created with 32 in it , ie now the total Entry node in all the link list can be 32.

i think case 2 is correct. please enplane the Re Hashing process, better if this diagram will be used.

解决方案

Rehashing increases the number of available buckets as a function of the number of entries currently stored in the HashMap.

It occurs when the HashMap implementation determines the number of buckets should be increased in order to maintain the expected O(1) lookup and insertion performance.

You are correct regarding the .75 default load factor and how it will cause the HashMap to be rehashed when the 13th entry is added.

However, it is not correct that default capacity of HashMap 16 means it can store 16 element in it. Any bucket can store multiple entries. However, in order to preserve the desired performance, the average number of entries per bucket should be small. That's the reason we have the load factor, and the reason we should use a proper hashCode() that spreads the keys as evenly as possible across the buckets.

这篇关于如何以及何时在HashMap中完成重新哈希处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:25