coding ++: java-HashMap load factor why the default is 0.75?

This article based on JDK1.8, especially in this description

1): the effect of the load factor

  Load factor is the expansion and mechanisms, meaning that if the capacity of the current container, up to the maximum set by us, we must begin the expansion operation. To give an example to explain, to avoid white did not understand:

For example, the current capacity of the container 16, a load factor of 0. The 75 , 16 * 0.75 = 12 , that is, when the capacity reaches 12 when it will be the expansion operation. 

Expansion mechanism is twice the size of the current capacity

His role is very simple, is equivalent to a threshold value expansion mechanism. When this threshold is exceeded, it will trigger the expansion mechanism. HashMap source has been specified load factor is 0.75 for us by default.

 

 I taken some source, from where it can be seen, the default value of the load factor is 0.75, but we can also specify to the constructor. Here we formally analyze why is the default 0.75.

2): explain the reasons (focus)

  When we consider the HashMap, HashMap first thought is just a data structure, since it is the most important data structure is to save time and space.

  The role of the load factor and certainly saves time and space. Why save it? We consider two extreme cases.

The load factor is 1.0:

  We look at the underlying data structure is HashMap:

Our data is initially stored in an array inside, when Hash collisions took place, is on the data node, give birth to a linked list, when the chain length reaches a certain length of time, it will put the list into a red-black tree.

 

When the load factor is 1.0 times, which means that only eight when the value of the array (this figure represents 8) all filled, the expansion will take place. 
This poses a big problem, because it can not be avoided when Hash collisions. When the load factor is 1.0 times, which means there will be a lot of Hash conflict, the underlying red-black trees become very complicated.
For the query efficiency is extremely unfavorable. This situation is to sacrifice the time to ensure the utilization of space.

 

So one sentence summary is that the load factor is too large, although the utilization of space up, but time efficiency is reduced.

 

The load factor is 0.5:

  When the load factor is 0.5, which means that, when the elements in the array reaches half began expansion, since less filling elements, Hash conflict will be reduced, then the height of the bottom of the chain length or is red-black tree it will be reduced.

  Query efficiency will increase.

  But, brothers, this time will be greatly reduced space utilization, originally stored data of 1M, 2M now would mean the need for space.

 

  Stating that the load factor is too small, although the time to enhance the efficiency, but space utilization is reduced.

 

Load factor is 0.75:

  After the previous analysis, basically Why is the answer will come out of 0.75, which is the trade-off of time and space.

  Of course, this answer is not my own imagination. The answer lies in the source code, we can see:

 

Generally meaning that when the load factor is 0.75, the space utilization is high, and to avoid a considerable number of Hash collision, so that the height of the bottom of the list or low red-black tree to enhance the space efficiency.

 

 

 

 

 

Citation: https://baijiahao.baidu.com/s?id=1656137152537394906&wfr=spider&for=pc

 

Guess you like

Origin www.cnblogs.com/codingmode/p/12593946.html