Analysis of implementation Essentials of HashMap in Java

Source: Internet
Author: User

@ Dream Blog The series of articles on the JDK common container classes ArrayList, LinkedList, HashMap, hashset and other implementation principles in the way of code comments are given (see http://www.cnblogs.com/ dongying/p/4022795.html#3045118 and so on), and I am here in another way to illustrate the main points of its implementation.

The implementation principle of ArrayList and linklist is relatively simple, and in the interview of Java, it is often required to write the simple implementation of these two kinds of container classes immediately. As the name suggests, Java support for generics uses arrays and two-way linked list storage elements internally, and expands when capacity is insufficient, so that the applicable scenarios for both container classes correspond to arrays and linked lists.

The implementation of HASHMAP uses the inner class entry, which is actually stored in HashMap by using an array of entry types, and entry is also a one-way headless list, which points to the next element with the next attribute. When you store a key-value pair, you first get the hash value of the key and calculate its storage location in the entry array, as shown in method Indexfor:

Static int indexfor (intint  length) {        return H & (Length-1);    }

The hash value and the entry array are sorted by bitwise AND operation to get the location index where they should be stored. The next process is the essence of it. If the location is empty, the entry object that represents the key-value pair is put in, otherwise there is a entry linked list at the location, and if the key value of the entry node is equal to the entry object to be inserted, replace the old value with the new value value and return the old one If there is no equal key value, the new entry object is placed at the end of the list.

It's a little messy. Summing up, this is actually a different key hash value of the same time conflict processing method. According to the principle of hash value, the hash value of the same element must be the same, the hash value of different elements is not necessarily the same. Conversely, elements with the same hash value are not necessarily the same, but the values of the elements with different hash values must be different. Therefore, the index values obtained by the different elements according to the hash value and the Indexfor () method are likely to be the same, so it is necessary to store all the elements in the index position for further screening with the linked list (it is more intuitive to display in the diagram). It can be inferred that the get () method of HashMap only guarantees approximate O (1) time complexity. The Loadfactor attribute in HashMap is also the reason why it exists. Further look at the source code discovery, Jdk8 in the implementation of the HASHMAP has changed, the same hash value of the key value pair is linked with the red and black tree to achieve the common implementation, the specific use of which structure is determined by the number of conflicting key values. The time complexity of this lookup is further optimized, but it also increases the time of storage, as it involves the conversion of linked lists and red and black trees, and the rebalancing of red and black trees.

And for HashSet can be regarded as a special use of HashMap, that is, all key values in the HashMap store the same externally invisible objects.

Analysis of implementation Essentials of HashMap 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.