HashMap specifies the initial capacity. For JDK computing of different versions, hashmapjdk

Source: Internet
Author: User

HashMap specifies the initial capacity. For JDK computing of different versions, hashmapjdk

The constructor of the specified initial capacity of HashMap:

public HashMap(int initialCapacity, float loadFactor) 

The initial capacity is based on the initialCapacity parameter to obtain the Npower of 2, which is equal to or greater than initialCapacity.

Capacity is the reason for N power of 2, see http://www.cnblogs.com/xwdreamer/archive/2012/06/03/2532832.html

 

1. In earlier JDK versions, the cyclic shift operation ensures that the initial capacity is 2 to the Npower.

public HashMap(int initialCapacity, float loadFactor) {      ......        int capacity = 1;      while (capacity < initialCapacity)          capacity <<= 1;        ...... }

 

2. This operation is optimized in JDK1.8.

public HashMap(int initialCapacity, float loadFactor) {    ......    this.threshold = tableSizeFor(initialCapacity);}static final int tableSizeFor(int cap) {    int n = cap - 1;     n |= n >>> 1;        n |= n >>> 2;       n |= n >>> 4;        n |= n >>> 8;    n |= n >>> 16;    return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;}

The 2.1 parameter is converted to binary instead of the Npower of 2.

1 xxxxxxxx

1000000000

Step 1: xxxxxxxx-1. Because there is at least one low position, the number of digits remains unchanged after the value is reduced by 1.

Step 2 n | = n >>> 1; 1 xxxxxxxx shifts one digit to 1 xxxxxxx, or changes to 11 xxxxxxx after calculation, no matter the low position

Step 3 n | = n >>> 2; 11 xxxxxxx shifts the right two digits to 11 xxxxx, or changes to 1111 xxxxx after calculation, regardless of the low position

Step 4 n | = n >>> 4; 1111 xxxxx shifts the four digits right to 1111x, or changes to 11111111x after the operation, regardless of the low position

......

In the end, we can ensure that all low positions are 1: 1 xxxxxxxx-> 111111111. + 1 is changed to 1000000000, which is the nth power greater than or equal to the minimum 2 of this number

Right Shift or operation, as of 16, can ensure that up to 32 bits are 1, because the maximum value of int type is 231-1, 31 bits

The 2.2 parameter is the Npower of 2

For example, after 1000, N-1 is changed to 111, after right shift or operation, it is still 111, and after + 1 is changed to 1000.

 

3. Comparison

In earlier versions, assume that the parameter is passed to the Npower of 2 and the comparison + displacement is calculated for a total of 2 * N times.

In JDK1.8, subtraction + displacement + or calculation, approximately 11 times

That is to say, after the specified array capacity is greater than 6 power (64) of 2, JDK1.8 is more efficient.

 

4. Optimization when passing the parameter to the Npower of 2

If n is not 1 and n-1 & n = 0, n is an integer power of 2.

JDK1.8 adding this judgment can reduce computing

Static final int tableSizeFor (int cap) {int n = cap-1; if (cap & n = 0) // return (cap> = MAXIMUM_CAPACITY) of parameter 2 )? MAXIMUM_CAPACITY: cap; n | = n >>> 1; n | = n >>> 2; n |=n >>> 4; n |=n >>> 8; n | = n >>> 16; return (n <0 )? 1: (n> = MAXIMUM_CAPACITY )? MAXIMUM_CAPACITY: n + 1 ;}

  

 

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.