Java's HashMap

Source: Internet
Author: User
Tags array length rehash

Hashmaphashmap principle?

Hash is a collection for storing Key-value key-value pairs, each key-value pair is also called entry, these entry are stored in an array, each element initial value is NULL, common method has Put,get

Put principle?

Put (1, "A")

1) computed array subscript index=hash (1) =hashcode (1) & (CAPACITY-1)

2) Insert array talbe[index].value= "A"

3) If Table[index] This position already has the value, uses the list to solve (Java8 to use the red black tree realizes)

Table[index]->next.value= "A"

Get principle?

Get (1, "A")

1) computed array subscript index=hash (1)

2) Get the data from the array Table[index]

3) If there is a conflict in this index, you need to traverse the linked list,

HashMap initial length?

The default initial length is 16, which must be a power of 2 for each auto-expansion or manual initialization. Why is it?

A hash function is used to calculate the array subscript according to key, so that the array subscripts obtained by the hash function are evenly distributed so that the hashmap is more efficient when put,get. The formula for calculating the subscript of an array is:

index = hashcode(key) & (capacity-1)# capacity是hashmap中数组的长度,假设使用默认长度key   hashcode(key)                           capacity-1  index1     49(?0011 0001?)                            1111       1 a     97(110 0001)                             1111       1book  3029737(?00101110001110101110 1001?)       1111       9apple 93029210(?010110001011100000110101 1010?)  1111       10# 保证HashMap数组长度是2的幂可保证capacity-1的二进制全是1,# 如果hash函数是均匀的话,得到的index也是均匀的
How to expand?

1) determine whether to enlarge

# 满足下列条件就需要扩容了HashMap.Size >= Capacity * LoadFactor# HashMap.Size表示HashMap中含有的元素个数# Capacity表示HashMap中数组的长度# LoadFactor表示负载因子,默认值为0.75f

2) Resize

Creates an empty array that is twice times the length of the original array.

Capacity = 2 * Capaicty

3) Rehash

The array length is changed, so the array subscript for each element may change, so it needs to be recalculated and added to the new array.

Non-thread safe

Concurrent rehash may result in conditional competition leading to circular links. See the reference link for specific analysis.

Look at the analysis in the reference article can say, but write or write it.

What's the difference with Java8 's hashmap?

There are hash collisions, such as two hashes that fall on the same index after modulo, or two different keys that have the same hash value.

JDK7 's practice is to build a linked list, and then insert the elements on top of each to perform the above judgment.

And JDK8 in the chain table length reached 8, and the number of barrels reached 64, built a red black tree, to solve the serious conflict performance problems.

Reference

Vaccines: The dead loop of JAVA hashmap-cool shells

Comic: What is HashMap?

Comics: HashMap under high concurrency

HashMap fully interprets-hollischuang ' s Blog

Java HashMap working principle and implementation | Yikun

How the HashMap works-importnew

Java's HashMap

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.