Java HashMap initial capacity value example

Source: Internet
Author: User


The underlying data length in HashMap is always 2 to the npower

When an element is stored in the underlying array of HashMap, the most direct way to determine its location is to modulo it so that it can be evenly distributed to the array. Here, we can calculate the hash value efficiently using h & (length-1) when the array length is 2 to the n power.

/**
* Returns index for hash code h.
*/
Static int indexFor (int h, int length ){
// Assert Integer. bitCount (length) = 1: "length must be a non-zero power of 2 ";
Return h & (length-1 );
}
By default, the initial capacity of HashMap is 16, and the load factor is 0.75. That is to say, only 12 elements can be stored. When you put 13th elements, you need to resize the array to 32.

Determine initial capacity

At first, the initial capacity is calculated manually to calculate the Npower of 2, which is greater than the data size. Of course, the loading factor should also be considered. In this way, every computation is very troublesome. You can set the load factor to 1 for simple computation. But check the source code. HashMap calculates the Size.

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 <= 0 | Float. isNaN (loadFactor ))
Throw new IllegalArgumentException ("Illegal load factor:" +
LoadFactor );
 
This. loadFactor = loadFactor;
Threshold = initialCapacity;
Init ();
}
/**
* Inflates the table.
*/
Private void inflateTable (int toSize ){
// Find a power of 2> = toSize
Int capacity = roundUpToPowerOf2 (toSize );
 
Threshold = (int) Math. min (capacity * loadFactor, MAXIMUM_CAPACITY + 1 );
Table = new Entry [capacity];
InitHashSeedAsNeeded (capacity );
}
 
Private static int roundUpToPowerOf2 (int number ){
// Assert number> = 0: "number must be non-negative ";
Return number> = MAXIMUM_CAPACITY
? MAXIMUM_CAPACITY
: (Number> 1 )? Integer. highestOneBit (number-1) <1): 1;
}

We can see that initialCapacity is assigned to threshold first, and threshold is the rehash flag. In inflateTable, the loadFactor is used to recalculate the value of threshold. Therefore, to avoid rehash during HashMap usage, initialCapacity cannot directly transmit the number of elements, and initialCapacity must be greater than or equal to (element length/loadFactor). Therefore, the load factor uses the default value of 0.75 and the initial capacity is set to 1.333... * Length, generally used to be 1.4 * length.

Function highestOneBit

When JDK1.6 is used, the while loop is used for continuous comparison with the npower of the smallest 2. The above changes were made in jdk1.7. First, take the highest digit of the number value, and then move it to the left. HighestOneBit implementation:


Public static int highestOneBit (int I ){
// HD, Figure 3-1
I | = (I> 1 );
I | = (I> 2 );
I | = (I> 4 );
I | = (I> 8 );
I | = (I> 16 );
Return I-(I >>> 1 );
}

After the shift, the I value starts from the highest bit 1 and is 1. The highest bit value is obtained after the right shift.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.