Directory
1. Introduction class Definition
public class hashmap<k,v>
extends abstractmap<k,v>
implements Map<k,v>, Cloneable, Serializable
1
2
3
Main introduction
HASHMAP implementations in JDK 1.7 and JDK 1.8 are significantly different today, I will be on the JDK 1.7 source, on this basis to explain the JDK 1.8 HashMap Source Analysis
Be sure to turn on JDK 1.7 control: Java: Hand-carried your source analysis HashMap 1.7 2. Data structure: Introduction of the red-black tree 2.1 main Introduction
About red and black trees understand: http://blog.csdn.net/v_july_v/article/details/6105630
2.2 Storage Process
Note: In order for us to have a perceptual understanding, simply draw the storage process, more detailed & specific storage process in the following source analysis
implementation class of 2.3 array elements & linked list nodes
The array elements & Link table nodes in HashMap are implemented by node class
Compared to JDK 1.7 (entry Class), it's just a change of name
The source analysis of this class is as follows
Detailed analysis Please read the note
/** * Node = HashMap Inner class, implements the Map.entry interface, essentially = a mapping (key-value pair) * Implements Getkey (), GetValue (), Equals (Object O) and hashcode () etc. * *
/Static Class Node<k,v> implements map.entry<k,v> {final int hash;//hash value, HashMap determine the location of the record based on this value Final K Key; Key V value; Value node<k,v> next;//list the next node//construct method nodes (int hash, K key, V value, node<k,v> n
EXT) {This.hash = hash;
This.key = key;
This.value = value;
This.next = Next; Public final K Getkey () {return key;} Returns the key that corresponds to this item public final v. GetValue () {return value}//returns the value that corresponds to this item public final String TOSTR
ing () {return key + "=" + value;}
Public final V SetValue (v newvalue) {v oldValue = value;
value = newvalue;
return oldValue;
}/** * Hashcode () */public final int hashcode () { return Objects.hashcode (key) ^ Objects.hashcode (value); /** * equals () * function: To determine whether 2 entry are equal, must be equal to the key and value, to return true * * */public final Boole
An Equals (Object o) {if (o = =) return true;
if (o instanceof map.entry) {map.entry<?,? > E = (map.entry<?,? >) o;
if (Objects.equals (Key, E.getkey ()) && objects.equals (value, E.getvalue ())
return true;
return false;
} 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 532.4 Red-Black tree node implementation classThe red-black tree node in HashMap is implemented by TreeNode class
/** * Red-Black tree node implementation class: Inherit from linkedhashmap.entry<k,v> class * * Static final class TREENODE<K,V&G T
Extends Linkedhashmap.entry<k,v> {//attribute = parent node, Zuozi, right subtree, delete auxiliary node + color treenode<k,v> parent;
Treenode<k,v> left;
Treenode<k,v> right;
Treenode<k,v> prev;
Boolean red;
constructor TreeNode (int hash, K key, V Val, node<k,v> next) {Super (hash, Key, Val, next); ///Returns the root node of the current node final treenode<k,v> root () {for treenode<k,v> R = this, p;;)
{if (p = r.parent) = = null) return r;
r = P;
} 1 2 3 4 5 6 7 8 9 of the .