We are preparing to review the recent Java, so here doing some records.
LinkedHashMap inherited HashMap, most of the operation invocation is to achieve a HashMap, during maintenance operations when more than a layer of a doubly linked list
LinkedHashMap node also inherits HashMap node, multi-node maintains the front and rear node two properties
1 static class Entry<K,V> extends HashMap.Node<K,V> { 2 Entry<K,V> before, after; 3 Entry(int hash, K key, V value, Node<K,V> next) { 4 super(hash, key, value, next); 5 } 6 }
1.put (K key, V value) to store a key-value pair, in fact, is to call the put method of HashMap to achieve the maintenance of the list by rewriting part of the method in the HashMap
. 1 the HashMap put the method generates a node, called newNode method, but this method LinkedHashMap override 2 / * 3 * Create a node . 4 * @param the hash value of the hash . 5 * @param Key button . 6 * @param value value . 7 * @param next node e, which is attribute node HashMap . 8 * @return . 9 * / 10 the node <K, V> the newNode ( int the hash, Key K, V value, the node <K, V> e) { . 11 // call the constructor 12 is LinkedHashMap.Entry <K, V> P = 13 is new newLinkedHashMap.Entry <K, V> (the hash, Key, value, E); 14 // maintenance list 15 linkNodeLast (P); 16 return P; . 17 } 18 is . 19 / ** 20 is * a node is added to the end of 21 * @ param P node 22 is * / 23 is Private void linkNodeLast (LinkedHashMap.Entry <K, V> P) { 24 // holding the tail node 25 LinkedHashMap.Entry <K, V> Last = tail; 26 is // updated node tail portion 27 tail = the p-; 28 //Before determining whether the node is empty tails 29 IF (Last == null ) 30 // before the tail node is empty, indicating that no data is provided at the head node 31 is head = P; 32 the else { 33 is // has been described previously data, the new node will be connected as the tail node 34 is p.before = Last; 35 last.after = P; 36 } 37 [ } 38 is 39 when an existing key put when triggered update operation if the HashMap, after afterNodeAccess method calls, LinkedHashMap override this method 40 / ** 41 is * accessOrder is true, the node will operate to move the tail of the linked list 42 is * @parame Node 43 is * / 44 is void afterNodeAccess (the Node <K, V> e) { 45 LinkedHashMap.Entry <K, V> Last; 46 is // accessOrder This parameter refers to the time during the operation of the node, whether the operation moves to Finally the list, the default false 47 // that is false when accessOrder insertion order list is maintained 48 // to true when the mobile node into the list of recently used will last 49 IF (accessOrder && (last = tail )! = E) { 50 // save the current node and its front and rear node node 51 is LinkedHashMap.Entry <K, V> P = 52 is (LinkedHashMap.Entry <K, V>) E, B = p.before , A = p.after; 53 is //Empty rear node as the current node to be moved to the last 54 is p.after = null ; 55 // determines whether the air pre-node node 56 is IF (B == null ) 57 is // preamble node is empty, indicating that the current node is the head node, the node it is post-second node is set to the head node 58 head = a; 59 the else 60 // presence front node, the node connecting the rear to the front of the node the current node the next node 61 is b.after = a; 62 is // determines whether the node is empty post 63 is iF (a =! null ) 64 // post node is not empty, the node updates the pre-post node 65 a.before = B; 66 the else 67 // indicating that the node is tail node, the node after node pre-set 68 // A p == null explanation is that the tail node? Bit unclear 69 Last = B; 70 // unified update the tail node 71 is IF (Last == null ) 72 // described have only one node 73 is head = P; 74 the else { 75 // current node linked to the end of the linked list 76 = p.before Last; 77 last.after = P; 78 } 79 // set the tail nodes 80 tail = p; 81 ++modCount; 82 } 83 } 84 85 LinkedHashMap也重写了afterNodeInsertion方法 86 void afterNodeInsertion(boolean evict) { 87 LinkedHashMap.Entry<K,V> first; 88 if (evict && (first = head) != null && removeEldestEntry(first)) { 89 K key = first.key; 90 removeNode(hash(key), key, null, false, true); 91 } 92 } 93 94 protected Boolean the removeEldestEntry (of Map.Entry <K, V> eldest) { 95 return to false ; 96 } 97 98 In fact, this method does not take effect in LinkedHashMap
2.get (Object key) Gets the value according to the key
1 / * 2 * get the value . 3 * @param Key button . 4 * @return . 5 * / . 6 public V GET (Object Key) { . 7 the Node <K, V> E; . 8 // call the HashMap method getNode 9 IF ((E = the getNode (the hash (Key), Key)) == null ) 10 return null ; . 11 IF (accessOrder) 12 is // node operation to move the current list the last 13 is afterNodeAccess (E); 14 return e.Value ; 15 }
3. containsValue (Object value) whether there is a value
1 public boolean containsValue(Object value) { 2 //通过遍历链表实现 3 for (LinkedHashMap.Entry<K,V> e = head; e != null; e = e.after) { 4 V v = e.value; 5 if (v == value || (value != null && value.equals(v))) 6 return true; 7 } 8 return false; 9 }
4.remove(Object key) 删除key
. 1 a LinkedHashMap remove method called HashMap 2 override the method afterNodeRemoval 3 / ** 4 * delete a node in the linked list . 5 * @param E . 6 * / . 7 void afterNodeRemoval (the Node <K, V> E) { . 8 // Get pre- and post the current node . 9 LinkedHashMap.Entry <K, V> P = 10 (LinkedHashMap.Entry <K, V>) E, B = p.before, a = p.after; . 11 // empty pre rear node 12 is p.before = p.after = null ; 13 is 14 IF (B == null ) 15 // Pre node is empty, illustrated as the head node, the node updates the header node for the post 16 head = A; . 17 the else 18 is // preamble node is not empty, the node front post is provided to the node of the deletion node rear node . 19 b.after = A; 20 is IF (A == null ) 21 is // post node is empty, illustrated as a tail node, the node updates its front tail node 22 is tail = B; 23 is the else 24 // rear node is not empty, the node updates the pre-post node 25 a.before = B; 26 is }