Linkedhashmap is a subclass of HashMap, and many places are directly referencing the methods in HashMap, so there are not many places to be aware of. The key point is a few overriding methods: 1, entry is the inheritance and node class, that is, the fundamental difference between Linkedhashmap and HashMap, node is a list form, only next and the next element to connect, The entry list has a before and after two connection points.
Static class extends Hashmap.node<k,v> { Entry<K,V> before, after; Entry (int hash, K key, V value, node<k,v> next) { super(hash, key, Value, next); } }
2, the difference is to change the node into entry, and follow the list of elements in an orderly connection
Node<k,v> newNode (int hash, K key, V value, node<k,v> e) { linkedhashmap.entry<k,v > P = new linkedhashmap.entry<k,v>(hash, key, value, e); Linknodelast (p); return p; }
3, the red and black tree node type has not changed, but also just follow the link list way to connect
Treenode<k,v> newtreenode (int hash, K key, V value, node<k,v> next) { TreeNode C14>new treenode<k,v>(hash, key, value, next); Linknodelast (p); return p; }
There are three reserved methods for Linkedhashmap in HashMap: 1, the first one is better understood,when you delete element E, the elements before and after e are connected.
voidAfternoderemoval (node<k,v> e) {//unlinkLinkedhashmap.entry<k,v> p =(Linkedhashmap.entry<K,V>) E, B = p.before, a =P.after; P.before= P.after =NULL; if(b = =NULL) Head=A; ElseB.after=A; if(A = =NULL) Tail=b; ElseA.before=b; }
2, this is the pit!
void afternodeinsertion (boolean// possibly remove eldest linkedhashmap.entry<k,v> first ; if null && removeeldestentry (first)) { = first.key; NULL false true ); } }
On the surface, this is a way to determine whether to delete the first element in a list, but ...
protected boolean removeeldestentry (map.entry<k,v> eldest) { returnfalse; }
This return value has always been false there is a wood there! There is nothing to see in this parameter. There's no way to get into this branch there are wood! I don't know what to do with this method. There is wood there! 3. A third methodThe main thing is to move the element to the last one in the list, the key parameter is Accessorder, this parameter is only in public linkedhashmap (int initialcapacity, float Loadfactor,boolean Accessorder) can be set to true manually, the rest of the time defaults to false, so this method is actually used in a relatively small place.
voidAfternodeaccess (node<k,v> e) {//Move node to the lastLinkedhashmap.entry<k,v>Last ; if(Accessorder && (last = tail)! =e) {linkedhashmap.entry<K,V> p =(Linkedhashmap.entry<K,V>) E, B = p.before, a =P.after; P.after=NULL; if(b = =NULL) Head=A; ElseB.after=A; if(A! =NULL) A.before=b; Else Last=b; if(Last = =NULL) Head=p; Else{P.before=Last ; Last.after=p; } Tail=p; ++Modcount; } }
Accessorder the actual meaning of this parameter is whether the control of the link table get is in the order of insertion or access order, such as the following example:
New Linkedhashmap (16,0.75f,true); M.put ("1", "a"); M.put ("2", "B"); M.put ("3", "C"); M.put ("4", "D"); M.get ("1"); M.get ("2"); M.foreach ((x, y)->{system.out.println (y);});
The output at this time is Cdab, and if the Accessorder is set to False then the output ABCD. This form of the list is actually called the Get method when there is no difference, there is no difference in storage mode, the difference is the method of foreach. HashMap want to traverse all the elements only through the three set of sets of iterators, but the linkedhashmap itself implements the Foreach method, and in the time of put the team elements are sorted, so you can directly traverse itself.
Linkedhashmap Source Reading notes (based on jdk1.8)