HashMap中负载因子的意义是什么? [英] What is the significance of load factor in HashMap?

查看:28
本文介绍了HashMap中负载因子的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashMap 有两个重要的属性:sizeload factor.我浏览了 Java 文档,它说 0.75f 是初始加载因子.但是我找不到它的实际用途.

HashMap has two important properties: size and load factor. I went through the Java documentation and it says 0.75f is the initial load factor. But I can't find the actual use of it.

谁能描述一下我们需要设置负载因子的不同场景以及不同情况下的一些样本理想值是什么?

Can someone describe what are the different scenarios where we need to set load factor and what are some sample ideal values for different cases?

推荐答案

文档 很好地解释了它:

HashMap 的实例有两个影响其性能的参数:初始容量和负载因子.容量是哈希表中的桶数,初始容量就是哈希表创建时的容量.负载因子是衡量哈希表在其容量自动增加之前允许达到多满的指标.当哈希表的条目数超过负载因子和当前容量的乘积时,重新哈希表(即重建内部数据结构),使哈希表的桶数大约增加一倍.

An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

作为一般规则,默认负载因子 (.75) 在时间和空间成本之间提供了很好的权衡.较高的值会减少空间开销,但会增加查找成本(反映在 HashMap 类的大多数操作中,包括 get 和 put).设置初始容量时应考虑映射中的预期条目数及其负载因子,以尽量减少重新哈希操作的次数.如果初始容量大于最大条目数除以负载因子,则不会发生重新哈希操作.

As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur.

与所有性能优化一样,最好避免过早优化(即没有关于瓶颈位置的硬数据).

As with all performance optimizations, it is a good idea to avoid optimizing things prematurely (i.e. without hard data on where the bottlenecks are).

这篇关于HashMap中负载因子的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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