Java HashMap learning notes

Source: Internet
Author: User

 

1. HashMap Data Structure

In java programming language, the most basic structure is two types, one is array, the other is analog pointer (reference ), all data structures can be constructed using these two basic structures, and HashMap is no exception. HashMap is actually a "linked list hash" data structure, that is, a combination of arrays and linked lists.

(Key = hash (key. hashCode ());
I = (Entry <K, V> e = table [I]; e! =; E = (e. hash = hash & (k = e. key) = key | ==++ indexFor (h, h & (length-1 hash (^ = (h >>> 20) ^ (h >>>> 12 h ^ (h >>>> 7) ^ (h >>> 4

Value is an Entry object:

  Entry<K,V>  Map.Entry<K,V><K,V> 

Next is the chained storage method, which aims to solve the hash conflict problem.

The following is an example:

Map <String, String> m = new HashMap <String, String> ();

M. put ("123", "abc ");

M. put ("i9", "def ");

Because the hashcodes of "123" and "i9" are different, they are 48690 and 3312 respectively, so they are not overwritten. However, the array subscript obtained through the indexFor method is 6 (the default capacity is large). When i9 is inserted, next is used.

Further description:

After execution of m. put ("123", "abc ");

MAXIMUM_CAPACITY = 1 <30 HashMap (initialCapacity, (initialCapacity <0 IllegalArgumentException ("Illegal initial capacity:" + (initialCapacity> =

According to the code above, the maximum capacity of hashMap is 1 <30, that is, 1073741824, because hashMap is based on arrays, And the subscript index of arrays is of the int type, therefore, the array size cannot be greater than Integer. MAX_VALUE is 231-1 (2147483647). Due to the size limit of the allocated memory, java is generally reported if the size is not as large. lang. outOfMemoryError: Java heap space error.

 

Refer:

Deep Analysis of Java HashMap in http://linxh83.iteye.com/blog/1403404

The internal implementation mechanism of HashMap in http://792881908-qq-com.iteye.com/blog/1447260

Implementation of Java HashMap in http://beyond99.blog.51cto.com/1469451/429789

Http://zha-zi.iteye.com/blog/1124484 hash algorithm (hashmap implementation principle)

Source code analysis of HashMap of http://blog.csdn.net/kiritor/article/details/8885961 Thinking in Java

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.