为什么HashMap初始化需要设定大小(数据量大的时候),HashMap初始化大小设定多少合适?

HashMap的介绍

在开始之前,先看下在官方文档中是如何介绍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.

    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.

翻译过来就是:
HashMap的实例有两个影响其性能的参数:初始容量和装载因子。容量是哈希表中的桶数,初始容量就是创建哈希表时的容量。负载因子是一种度量方法,用来衡量在自动增加哈希表的容量之前,哈希表允许达到的满度。当哈希表中的条目数超过负载因子和当前容量的乘积时,哈希表将被重新哈希(即重新构建内部数据结构),这样哈希表的桶数大约是原来的两倍。

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

上面的说法总结一下就是:HashMap的扩容条件就是当HashMap中的元素个数(size)超过临界值(threshold)时就会自动扩容。在HashMap中,threshold = loadFactor * capacity。扩容时新的capacity *= 2。
代码说明初始化的好处

通过上面的说明可以看出,初始容量是影响性能的一个方面,通过代码来直观的感受下:

 

 所以初始化需要设定大小(数据量大的时候)!!!!设定多少合适请参看:HashMap初始化大小设定多少合适?

posted @ 2023-01-07 10:03  锐洋智能  阅读(189)  评论(0编辑  收藏  举报