Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the reason why the initial value of load factor of HashMap is 0.75?

Shulou Source: shulou.com Published: 2022-06-03 05:05:21 10月24日 Update

This article mainly explains "what is the reason why the initial value of load factor of HashMap is 0.75". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the reason why the initial value of load factor of HashMap is 0.75".

I. the role of load factor

For the study of HashMap, I have been thinking about how the source code is implemented, and now when I look at it again, I find that the default parameter values of the system are the essence of HashMap.

The load factor is related to the capacity expansion mechanism, which means that if the capacity of the current container reaches the maximum value set by us, the expansion operation will be started. Give an example to explain, in case Xiaobai doesn't understand:

For example, the current container capacity is 16, and the load factor is 0.75, 16, 0.75, 12, that is, when the capacity reaches 12, the capacity will be expanded.

His role is very simple, equivalent to a threshold for the expansion mechanism. When this threshold is exceeded, the expansion mechanism will be triggered. The HashMap source code has assigned us a load factor of 0.75 by default.

Public class HashMap extends AbstractMap implements Map, Cloneable, Serializable {/ / slightly static final float DEFAULT_LOAD_FACTOR = 0.75f; / / slightly public HashMap (int initialCapacity, float loadFactor) {if (initialCapacity)

< 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity >

MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor

Tags: Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei Linux OPPO Reno MariaDB