The LinkedHashMap class in the Java set, javalinkedhashmap

Source: Internet
Author: User

The LinkedHashMap class in the Java set, javalinkedhashmap

Jdk1.8.0 _ 144

To read this article, we 'd better first understand the underlying layer of HashMap. You can go to the HashMap class in the Java set.

LinkedHashMapInsert orderIt is also a commonly used Map set. It inherits HashMap, and many methods directly reuse the HashMap method of the parent class. This article will discuss the internal implementation of LinkedHashMap and how it ensures that the inserted elements are sorted by the insertion order.

Before analysis, you can think about it. Since it is based on the insertion order and starts with Linked-, it is likely to be the implementation of the Linked list. If it is implemented purely by a linked list, it is not impossible. LinkedHashMap maintains a linked list internally, inserts an element, encapsulates it as an Entry node, and inserts it to the end of the linked list. Functions can be implemented, but the search efficiency has reached O (n), which is much more time complex than O (1) in the case of no conflict in HashMap. This does not reflect the advantages of the random access to Map data structures.

Therefore, it is obvious that LinkedHashMap cannot have only one linked list to maintain Entry nodes. It is very likely to maintain two data structures: Hash list + linked list.

For ease of understanding, each method is not analyzed, and the LinkedHashMap data structure and implementation are analyzed starting from the insertion.

LinkedHashMap inherits the HashMap class and does not overwrite the put method. Instead, it directly follows the HashMap # put method. HashMap # put has been described in detail in Java set HashMap class. From calling the HashMap # put method, we can see that its insertion process is the same as that of HashMap, that is, it also has the same hash list structure as HashMap. However, be careful that although HashMap # put method is called, one of the methods in this method is to construct a new node newNode. Here, javashashmap is overwritten, so it calls javashashmap # newNode, this method implements maintenance of the LinkedHashMap linked list.

Ignore other code. The key code is tab [I] = newNode (hash, key, value, null) in HashMap # putVal. You can check the LinkedHashMap # newNode method later.

The process is illustrated by a legend.

1 // LinkedHashMap # newNode, construct a new Node 2 Node <K, V> newNode (int hash, K key, V value, Node <K, V> e) {3 LinkedHashMap. entry <K, V> p = 4 new LinkedHashMap. entry <K, V> (hash, key, value, e); 5 linkNodeLast (p); 6 return p; 7}

1 // LinkedHashMap # linkNodeLast, inserted to the end of the linked list 2 private void linkNodeLast (LinkedHashMap. entry <K, V> p) {3 LinkedHashMap. entry <K, V> last = tail; // LinkedHashMap defines the tail pointer and head pointer, and the linked list is a two-way linked list 4 tail = p; 5 if (last = null) 6 head = p; 7 else {8 // insert 9 p to the two-way linked list. before = last; 10 last. after = p; 11} 12}

For LinkedHashMap insertion, the hash part and the HashMap part are the same, while the two-way linked list part is inserted at the end to ensure the insertion order.

The internal implementation of LinkedHashMap is basically understood through insertion. The get method is very simple. It is also used to calculate the hash of the key and the subscript of the corresponding hash list.

Three methods need to be mentioned in LinkedHashMap. These three methods are defined in HashMap, but they are not implemented in detail. The specific implementation is put in LinkedHashMap.

Void afterNodeAccess (Node <K, V> p)

This method supports sequential access. If accessOrder = true is defined in the method, the accessed (get) elements are placed at the end of the linked list. AccessOrder settings can be passed through the constructor.

Void afterNodeInsertion (boolean evict)

This method is meaningless in LinkedHashMap, because the removeEldestEntry it calls always returns false, and the program will return and will not execute. However, if you override the removeEldestEntry method, you can implement the LRU (least recently used) cache.

Void afterNodeRemoval (Node <K, V> p)

Call when removing elements from the Map to update the two-way linked list.

 

 

This is a public number that can add buff to programmers.

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.