jdk1.8.0_144
It is best to read the bottom of the HashMap first and go to the HashMap class in the Java collection.
Linkedhashmap is a more commonly used map collection because of its insertion order characteristics. It inherits the HashMap, and many methods have directly reused the method of the parent class HashMap. This article explores the internal implementation of LINKEDHASHMAP and how it guarantees that the inserted elements are sorted in the order in which they are inserted.
Before the analysis can be considered, since it is in the order of insertion, and start with linked-, it is likely that the list is implemented. If it is implemented purely as a linked list, it is not possible, Linkedhashmap maintains a linked list internally, inserts an element into it and encapsulates it as a entry node and inserts it into the tail of the list. Functionality can be achieved, but this brings the search efficiency up to O (n), which is clearly far greater than the time complexity of O (1) In the absence of conflict in the case of HashMap. This does not reflect the map of this data structure random access to the advantages of fast.
So obviously, Linkedhashmap cannot have only one linked list to maintain the entry node, it is very likely to maintain two kinds of data structures: Hash list + chain list.
For ease of understanding, each method will not be analyzed, and the data structure and implementation of LINKEDHASHMAP will be analyzed from the beginning of the insertion.
Linkedhashmap inherits the HashMap class, and instead of rewriting the Put method, it follows the Hashmap#put method directly. The hashmap#put has been described in more detail in the HashMap class in the Java collection. From calling the Hashmap#put method, it is the same as the hashmap, which means that it has the same hash list structure as the HashMap. But be careful. Although the Hashmap#put method is called, one of the methods in this method is to construct a new node NewNode, where Linkedhashmap is rewritten, so Linkedhashmap#newnode is called, It is this method that realizes the maintenance of the Linkedhashmap chain list.
Ignore the rest of the code, the key code in Hashmap#putval tab[i] = NewNode (hash, key, value, NULL), and then see the Linkedhashmap#newnode method later.
The process is first illustrated with a legend.
The list insertion process is shown in the following code:
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, inserting into the tail of the list2 Private voidLinknodelast (linkedhashmap.entry<k,v>p) {3Linkedhashmap.entry<k,v> last = tail;//Linkedhashmap defines the tail tail pointer and head head pointer, and the linked list is a doubly linked list4Tail =p;5 if(Last = =NULL)6Head =p;7 Else {8 //inserting a doubly linked list9P.before =Last ;TenLast.after =p; One } A}
For Linkedhashmap inserts, the hash list portion is consistent with the hashmap, while the doubly linked list part is inserted to the tail, which ensures that the insertion order is maintained.
By inserting a basic understanding of the internal implementation of the Linkedhashmap, the Get method is simple, the same is calculated key hash and the corresponding hash table subscript can be.
There are three methods that need to be mentioned in Linkedhashmap, the three methods are defined in HashMap, but they are not implemented specifically, and the implementation is put into linkedhashmap.
void Afternodeaccess (Node<k,v> p)
This method can be sorted by access order, and if accessorder=true is defined in the method, the access (get) element is placed at the end of the list. Accessorder settings can be passed by construction methods.
void Afternodeinsertion (Boolean evict)
This method does not make sense in Linkedhashmap, because the removeeldestentry that it calls always returns false, and the program returns to not execute. However, if you override the Removeeldestentry method, you can implement the LRU (least recently used) cache.
void Afternoderemoval (Node<k,v> p)
Called when an element in a map is removed, updating a doubly linked list.
This is a public number that can give the programmer a buff.
Linkedhashmap classes in the Java collection