LinkedHashMap from entry to soil

Learn more about LinkedHashMap

Introduction

​ It can be regarded as HashMap+LinkedList, which not only uses HashMap to manipulate the data structure, but also uses LinkedList to maintain the sequence of inserted elements. It uses a bidirectional form to connect all elements internally.

  • Inherited from HashMap, most methods are not rewritten, such as remove and put methods

  • A key is reinserted, the order is not affected

  • Asynchronous, you can call Collections to achieve class synchronization

  • Assess-ordered structural changes will respond to the order of traversal

  • Iterative order

  • There is one more maintenance of a doubly linked list than HashMap, and most methods are implemented by HashMap.

  • The bottom layer is a hash table plus a doubly linked list

  • The order of insertion is ordered (the underlying linked list leads to order)

  • It can be used to implement the latest LRU algorithm, which is a commonly used page replacement algorithm.

  • Provides the algorithm of map application, which is allowed to be null, not synchronized, you can call Collection to achieve synchronization

  • Like HashMap, the initial capacity and loading factor have a great influence on LinkedHashMap, but the initial capacity is not subject to order when it traverses.

  • It can set two traversal sequences:

    • Visit order
    • Insertion order (default)

Structure diagram

First look at the class inheritance diagram of LinkedHashMap:

[External link image transfer failed. The source site may have an anti-leech chain mechanism. It is recommended to save the image and upload it directly (img-h2DaVFWu-1595400107143)(https://raw.githubusercontent.com/iszhonghu/Picture-bed/master/img /20200721111121.png)]

analysis

parameter

  • The Node node that inherits HashMap, it is a doubly linked list, including pre-pointers and post-pointers
  • When building a new node, it is LinkedHashMap. Entry is no longer Node.

method

Construction method
  • The construction method of HashMap is called, and the insertion order is used by default.
put method

Without rewriting, the put method of HashMap is directly called. It's just that when creating a node, the method overridden by LinkedHashMap is called.

get method
 public V get(Object key) {
    
    
        Node<K,V> e;
        if ((e = getNode(hash(key), key)) == null)
            return null;
        if (accessOrder)
            afterNodeAccess(e);
        return e.value;
    }
  • Call the method defined by HashMap to get the corresponding node
  • If it is the access sequence, put the node at the end of the linked list
remove method

LinkedHashMap does not override the remove method, it just overrides the afterNodeRemoval method.

Traverse method

​ Start looping output from the header of the internally maintained double-linked list, because the traversal is a doubly-linked list, not a hash table, so the initial capacity has no effect on the traversal

common problem

Guess you like

Origin blog.csdn.net/issunmingzhi/article/details/107513189