Judging the Java programmer level, HashMap is enough.

Source: Internet
Author: User
Tags bitwise

JDK1.8 HashMap Source Analysis

Symbols used:

^ XOR: Two operands are the same, the result is; two operands are different, the result is 1.

& Bitwise AND: Two operands are 1, the result is 1.

I. Overview of HASHMAP

Prior to JDK1.8, HashMap was implemented using an array + chain list, which uses a list to handle conflicts, and a linked list of the same hash value is stored in a list. However, when there are more elements in a bucket, that is, if the hash value is more than one element, it is inefficient to find the key value in turn. In JDK1.8, HashMap uses array + linked list + red-black tree (binary tree optimization is a balanced binary tree, can reduce the depth of the number of) implementation, when the list length exceeds the threshold (8), the linked list into a red black tree, which greatly reduces the time to find.

jdk1.8 before the HashMap are used in the structure, are based on an array and multiple single-linked lists, hash value conflict, the corresponding node is stored in the form of a list. If you look for one of the nodes in a linked list, you will spend an O (n) lookup time with a significant performance penalty. By jdk1.8, when the number of nodes with the same hash value is not less than 8 o'clock, it is no longer stored as a single-linked table, but instead uses a red-black tree.

Second, understand the hash function

Let's look at the source of the hash first:

Code execution procedure: If key is empty, returns 0, if key is not empty, returns the original hash value and the original hash value unsigned right shift 16 bit the value of the bitwise XOR result. (The low 16-bit and high 16-bit are different or operation). Because the low repetition probability is large, low and high XOR can commit the utilization of the array and make the array evenly distributed.

Bitwise XOR is to put two numbers in binary, the same takes 0, the difference is taken 1.

For example: 0101 ^ 1110 results for 1011, XOR speed is very fast.

To move a number to the right 16 bits is discarded low 16, that is, any number less than 216, the right to 16 after the result is 0 (2 16 of the square and then the right shift is just 1).

Any one number, with 0 bitwise XOR, is the result of this number itself.

So the hash () function will only readjust its value if it is greater than or equal to 216 for a non-null hash value.

But what good is the adjustment?

Let's first look at the put, the hash value is how to deal with (part of the source code):

When looking for a bucket, this hash value is the zise-1 with the table above, the initial is 16, we take 16来 example.

Think that the algorithm is HashValue & Size-1, at this time size-1=15 binary is 1 1 1 1, that is, any like 16 binary 0x?0 (binary last four bits 0000) hash value, will be stored in the position of 0 bucket, a bucket of too many elements, Will certainly degrade its performance. We know that HashMap is an array + linked list, is the subscript in the same position under the list of a single item, the array of subscript needs to be calculated, must be guaranteed in a certain range, is through the "(n-1) &hash" Computer computing is binary, such as the size of the array must be 2 n power , the weight is 0.75, the array initial value is 16, the weight equals 12 (16*0.75), and when equals 12 the array expands. After expansion, the array is redistributed. When the size of the list is greater than 8, it turns into a red-black tree. Array subscript calculation method: (0101010101010101010101010101&00000000000000000001111) with 16来, hexadecimal binary means that the binary end of the 0,n-1 is 1111, Both the 1111,1111 and the operation maximum are 166 binary is 15, then put the node nodes in this position, so as to calculate the subscript.

Summarized as follows:

Judging the Java programmer level, HashMap is enough.

Related Article

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.