What is the maximum capacity of HashMap for 2 of the 30-time square?

Source: Internet
Author: User

Looking at the bottom of HashMap today, we found that the maximum capacity of HashMap is as follows:

    1. Maximum capacity (must be a power of 2 and less than 2 30 times, the incoming capacity is replaced by this value)

    2. static final int maximum_capacity = 1 << 30;

When you see the 1<<30, the "<<" a little vague, when the understanding of "<<" after the use of a problem;

The int type is not 4 bytes altogether 32 bits, why not 1<<31?

First introduce the meaning of the numbers and characters on the right side of the equals sign:

1, "<<" is the left-shift operator, 1 means "1" in decimal, 30 means that the decimal number 1 is converted to binary and the left 30 bits are moved. The value is equal to 2 of the power of 30.

2, why is 2 of 30 power?

Take one byte for example: 1<<2 = 4

0000 0001 (Decimal 1)

Moves two bits to the left and then becomes

0000 0100 (Decimal 4)

The visible 1<<30 equals the 30 powers of 2 in decimal.

Back to the question, why is 2 of the power of 30, rather than 2 of the power of 31 times?

First: Java specifies that the static variable of the static final type is of type int, as to why it is not a byte, long, and so on, because of the compromise that takes into account HashMap performance issues!

Because the int type restricts the length of the variable to 4 bytes in a total of 32 bits, it is arguably possible to move the 31-bit 2 power to the left. But in fact, because the highest one in the binary number is the leftmost one is the sign bit, used to indicate the positive and negative points (0 is positive, 1 is negative), so can only move to the left 30 bits, and cannot move to the highest level of the sign bit!

---------------------This article from the love Code Monk's Csdn blog, full-text address please click: 80139620?utm_source=copy

What is the maximum capacity of HashMap for 2 of the 30-time square?

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.