哈希表简介 在哈希表中进行添加,删除,查找等操作,性能十分之高,不考虑哈希冲突的情况下(后面会探讨下哈希冲突的情况),仅需一次定位即可完成,时间复杂度为O(1),接下来我们就来看看哈希表是如何实现达到惊艳的常数阶O(1)的。
我们知道,数据结构的物理存储结构只有两种:顺序存储结构 和链式存储结构 (像栈,队列,树,图等是从逻辑结构去抽象的,映射到内存中,也这两种物理组织形式),而在上面我们提到过,在数组中根据下标查找某个元素,一次定位就可以达到,哈希表利用了这种特性,哈希表的主干就是数组 。
比如我们要新增或查找某个元素,我们通过把当前元素的关键字 通过某个函数(散列函数,哈希函数)映射到数组中的某个位置,通过数组下标一次定位就可完成操作。 
这个函数可以简单描述为:存储位置 = f(关键字)  ,这个函数f一般称为哈希函数,这个函数的设计好坏会直接影响到哈希表的优劣。
我们第一时间能想到的最简单的哈希函数就是取余%操作
例如我新增或查找某个元素 ,当前数组的长度为8,我们按照存储位置=f(关键字)。散列函数f就为f(i%8)(范围为[0,7]正好是数组的索引范围)。
同理当我们想要获取到元素时,只需要把i传过去然后通过散列函数计算
但是可以看到这样散列函数的方式是存在一个问题的!!按照上图所示,当我们的i=10的时候通过哈希函数得出的实际存储地址相同 。也就是说,当我们对某个元素进行哈希运算,得到一个存储地址,然后要进行插入的时候,发现已经被其他元素占用了,其实这就是所谓的哈希冲突 ,也叫哈希碰撞 。
前面我们提到过,哈希函数的设计至关重要,好的哈希函数会尽可能地保证 计算简单和散列地址分布均匀,但是,我们需要清楚的是,数组是一块连续的固定长度的内存空间,再好的哈希函数也不能保证得到的存储地址绝对不发生冲突。那么哈希冲突如何解决呢?
哈希冲突的解决方案有多种:开放定址法(发生冲突,继续寻找下一块未被占用的存储地址),再散列函数法,链地址法,而HashMap即是采用了链地址法 ,也就是数组+链表 (JDK7)的方式。
JDK7 JDK7版本的HashMap底层采用的是链地址法,也就是数组+链表的方式。 
HashMap的主干是一个Entry数组。Entry是HashMap的基本组成单元,每一个Entry包含一个key-value键值对。(其实所谓Map其实就是保存了两个对象之间的映射关系的一种集合)
1 2 3 transient  Entry<K,V>[] table = (Entry<K,V>[]) EMPTY_TABLE;
HashMap的主干数组,可以看到就是一个Entry数组,初始值为空数组{}。 
主干数组的长度(HashMap长度)一定是2的次幂。 
Entry是HashMap中的一个静态内部类。代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 static  class  Entry <K ,V > implements  Map .Entry <K ,V >     final  K key;     V value;     Entry<K,V> next;     int  hash;          Entry(int  h, K k, V v, Entry<K,V> n) {         value = v;         next = n;         key = k;         hash = h;     }  
所以,HashMap的总体结构如下:
简单来说,HashMap由数组+链表组成的 ,Entry数组是HashMap的主体,链表则是主要为了解决哈希冲突而存在的,如果定位到的数组位置不含链表(当前entry的next指向null),那么查找,添加等操作很快,仅需一次寻址即可;如果定位到的数组包含链表,对于添加操作,其时间复杂度为O(n),首先遍历链表,存在即覆盖,否则新增;对于查找操作来讲,仍需遍历链表,然后通过key对象的equals方法逐一比对查找。所以,性能考虑,HashMap中的链表出现越少,性能才会越好。 
其他几个重要字段
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 static  final  int  DEFAULT_INITIAL_CAPACITY = 1  << 4 ; transient  int  size;int  threshold;final  float  loadFactor;transient  int  modCount;
构造器 HashMap有4个构造器,其他构造器如果用户没有传入initialCapacity 和loadFactor这两个参数,会使用默认值。
initialCapacity默认为16,loadFactory默认为0.75
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 53 54 55 56 57 58 59 60  public  HashMap (int  initialCapacity, float  loadFactor)        if  (initialCapacity < 0 )          throw  new  IllegalArgumentException("Illegal initial capacity: "  +                                             initialCapacity);      if  (initialCapacity > MAXIMUM_CAPACITY)          initialCapacity = MAXIMUM_CAPACITY;      if  (loadFactor <= 0  || Float.isNaN(loadFactor))          throw  new  IllegalArgumentException("Illegal load factor: "  +                                             loadFactor);      this .loadFactor = loadFactor;      threshold = initialCapacity;      init();  }    public  HashMap (int  initialCapacity)        this (initialCapacity, DEFAULT_LOAD_FACTOR);  }    public  HashMap ()        this (DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);  }    public  HashMap (Map<? extends K, ? extends V> m)        this (Math.max((int ) (m.size() / DEFAULT_LOAD_FACTOR) + 1 ,                    DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);      inflateTable(threshold);      putAllForCreate(m);  } 
我们以默认构造器为例查看一下源码。
1 2 3 4 public  HashMap ()           this (DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); } 
这里调用的是另一个重载构造器public HashMap(int initialCapacity, float loadFactor)
这构造方法主要就是设置赋值负载因子loadFactor默认值为0.75,设置阈值threshold,当前为16。threshold一般为capacity *loadFactory
init方法在HashMap中没有实际实现,不过在其子类如 linkedHashMap中就会有对应实现 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public  HashMap (int  initialCapacity, float  loadFactor)                if  (initialCapacity < 0 )             throw  new  IllegalArgumentException("Illegal initial capacity: "  +                                                initialCapacity);         if  (initialCapacity > MAXIMUM_CAPACITY)             initialCapacity = MAXIMUM_CAPACITY;         if  (loadFactor <= 0  || Float.isNaN(loadFactor))             throw  new  IllegalArgumentException("Illegal load factor: "  +                                                loadFactor);         this .loadFactor = loadFactor;         threshold = initialCapacity;               init();     } 
从上面这段代码我们可以看出,在常规构造器中,没有为数组table分配内存空间(有一个入参为指定Map的构造器例外),而是在执行put操作的时候才真正构建table数组 
1 2 3 4 5 6 7 public  HashMap (Map<? extends K, ? extends V> m)      this (Math.max((int ) (m.size() / DEFAULT_LOAD_FACTOR) + 1 ,                   DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);     inflateTable(threshold);     putAllForCreate(m); } 
put() 我们在看看put()方法
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 public  V put (K key, V value)                            if  (table == EMPTY_TABLE) {             inflateTable(threshold);         }                 if  (key == null )             return  putForNullKey(value);         int  hash = hash(key);         int  i = indexFor(hash, table.length);         for  (Entry<K,V> e = table[i]; e != null ; e = e.next) {                      Object k;             if  (e.hash == hash && ((k = e.key) == key || key.equals(k))) {                 V oldValue = e.value;                 e.value = value;                 e.recordAccess(this );                 return  oldValue;             }         }         modCount++;         addEntry(hash, key, value, i);         return  null ;     } 
此时我们在去看看inflateTable()方法
inflateTable这个方法用于为主干数组table在内存中分配存储空间 
1 2 3 4 5 6 7 8 private  void  inflateTable (int  toSize)          int  capacity = roundUpToPowerOf2(toSize);                  threshold = (int ) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY + 1 );         table = new  Entry[capacity];         initHashSeedAsNeeded(capacity);     } 
roundUpToPowerOf2中的这段处理使得数组长度一定为2的次幂,Integer.highestOneBit是用来获取最左边的bit(其他bit位为0)所代表的数值.
1 2 3 4 5 6 7 private  static  int  roundUpToPowerOf2 (int  number)                 return  number >= MAXIMUM_CAPACITY                ? MAXIMUM_CAPACITY                : (number > 1 ) ? Integer.highestOneBit((number - 1 ) << 1 ) : 1 ;    } 
接着我们在回头看put中的hash()
这是一个神奇的函数,用了很多的异或,移位等运算对key的hashcode进一步进行计算以及二进制位的调整等来保证最终获取的存储位置尽量分布均匀。 
1 2 3 4 5 6 7 8 9 10 11 12 13 final  int  hash (Object k)          int  h = hashSeed;         if  (0  != h && k instanceof  String) {             return  sun.misc.Hashing.stringHash32((String) k);         }         h ^= k.hashCode();         h ^= (h >>> 20 ) ^ (h >>> 12 );         return  h ^ (h >>> 7 ) ^ (h >>> 4 );     } 
以上hash函数计算出的值,通过indexFor()进一步处理来获取实际的存储位置
1 2 3 4 static  int  indexFor (int  h, int  length)           return  h & (length-1 ); } 
h & (length-1);这里的length传入的也就是HashMap的数组长度,当前为16。但是它是怎么保证通过该方法得到的数组下标是符合数组下标范围的呢?也就是[0,15]
假设我们的得到的h二进制为0101 0101,length为16二进制为0001 0000,减1变成15二进制变成了0000 1111此时再与h做&运算,得到的数也就是h二进制的后四位数0101。而h是根据key的hashCode方法和hash()方法,是具有随机性的!但是只要我们保证HashMap的数组长度为2的次方,例如这里得到的索引下标范围就是二进制的[0000,1111]也就是[0,15]。从而保证了下标的范围的合理性! 
同样有一个问题,为啥不使用我们之前设想过的%操作,而是使用&操作呢??
这是因为&在任何操作系统上都是比较二进制位的操作。所以都不会慢,效率远比%高~ 
得到了我们想要的数组下标之后,我们在来看看put()方法中的后续操作。
for循环中的我们首先可以不用查看,我们可以看到调用了addEntry()方法
1 2 3 4 5 6 7 8 9 void  addEntry (int  hash, K key, V value, int  bucketIndex)          if  ((size >= threshold) && (null  != table[bucketIndex])) {             resize(2  * table.length);             hash = (null  != key) ? hash(key) : 0 ;             bucketIndex = indexFor(hash, table.length);         }         createEntry(hash, key, value, bucketIndex);     } 
size>=threshold HashMap的数组元素个数已经大于等于12。且当前要添加的数组下标位置已经存在元素了(准备添加时发生哈希冲突)。resize(2*table.length)将其扩容2倍
我们来看看resize()方法
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 void  resize (int  newCapacity)           Entry[] oldTable = table;          int  oldCapacity = oldTable.length;     if  (oldCapacity == MAXIMUM_CAPACITY) {         threshold = Integer.MAX_VALUE;         return ;     }          Entry[] newTable = new  Entry[newCapacity];          transfer(newTable, initHashSeedAsNeeded(newCapacity));          table = newTable;          threshold = (int )Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1 ); } void  transfer (Entry[] newTable, boolean  rehash)      int  newCapacity = newTable.length;          for  (Entry<K,V> e : table) {         while (null  != e) {             Entry<K,V> next = e.next;             if  (rehash) {                 e.hash = null  == e.key ? 0  : hash(e.key);             }                          int  i = indexFor(e.hash, newCapacity);             e.next = newTable[i];             newTable[i] = e;             e = next;         }     } } 
transfer()方法,将旧Entry数组重新计算数组索引,添加到新Entry数组。这里添加的位置是有两种的!,一种是原来的位置,一种是原来位置+原先数组长度。为什么是这个两个位置呢?我们来分析一下~还是使用之前分析的假设值
说完扩容resize方法,回到addEntry方法中,查看createEntry()方法
1 2 3 4 5 6 7 8 void  createEntry (int  hash, K key, V value, int  bucketIndex)           Entry<K,V> e = table[bucketIndex];          table[bucketIndex] = new  Entry<>(hash, key, value, e);          size++; } 
到此为此为止我们HashMap终于通过构造器创建,并成功添加一个元素啦。
我们再回到put() 方法。看一下该for循环。
该循环是遍历该数组下标位置的链表,判断是否存在相同Key,存在则覆盖值value,返回旧值。
e.hash == hash && ((k = e.key) == key || key.equals(k))判断键是否相同。
hash值不相等,对象一定不相等。
hash值相等,对象不一定相等。
两键是同一个对象,首先比较两个的hash值是否相同,如果相同再比较两个的内存地址值是否相同。
或者equals()方法比较是否相等。
HashMap是允许我们添加一个键为null的键值对的。 
当我们的键为null添加的时候调用的是putForNullKey()方法。
在数组下标为0的位置添加null键值对,添加前同样遍历一遍,看是否存在相同键。存在覆盖值,返回旧值。 
①如果定位到的数组位置没有元素 就直接插入。 
②如果定位到的数组位置有元素,遍历以这个元素为头结点的链表,依次和插入的key比较,如果key相同就直接覆盖,不同就采用头插法插入元素。 
 
get() 分析了一遍put(),之后再看get()就会简单许多。
1 2 3 4 5 6 7 8 9 public  V get (Object key)           if  (key == null )         return  getForNullKey();          Entry<K,V> entry = getEntry(key);          return  null  == entry ? null  : entry.getValue(); } 
1 2 3 4 5 6 7 8 9 10 11 12 13 private  V getForNullKey ()           if  (size == 0 ) {         return  null ;     }          for  (Entry<K,V> e = table[0 ]; e != null ; e = e.next) {                  if  (e.key == null )             return  e.value;     }     return  null ; } 
查看源码可以得知最重要的是调用getEntry()方法,我们来查看这个方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 final  Entry<K,V> getEntry (Object key)           if  (size == 0 ) {         return  null ;     }          int  hash = (key == null ) ? 0  : hash(key);          for  (Entry<K,V> e = table[indexFor(hash, table.length)];          e != null ;          e = e.next) {         Object k;                 if  (e.hash == hash &&             ((k = e.key) == key || (key != null  && key.equals(k))))                          return  e;     }     return  null ; } 
到此为止JDK7版本的HashMap核心源码基本分析完成。
JDK8 在JDK8中Entry对象名字变成了Node。底层数据结构也不单单是数组+链表而是会变成数组+红黑树
1 2 3 4 5 6 7 8 9 10 11 final  int  hash;final  K key;V value; Node<K,V> next; Node(int  hash, K key, V value, Node<K,V> next) {     this .hash = hash;     this .key = key;     this .value = value;     this .next = next; } 
参数 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 public  class  HashMap <K ,V > extends  AbstractMap <K ,V > implements  Map <K ,V >, Cloneable , Serializable           private  static  final  long  serialVersionUID = 362498820763181265L ;              static  final  int  DEFAULT_INITIAL_CAPACITY = 1  << 4 ;             static  final  int  MAXIMUM_CAPACITY = 1  << 30 ;           static  final  float  DEFAULT_LOAD_FACTOR = 0.75f ;          static  final  int  TREEIFY_THRESHOLD = 8 ;           static  final  int  UNTREEIFY_THRESHOLD = 6 ;          static  final  int  MIN_TREEIFY_CAPACITY = 64 ;          transient  Node<k,v>[] table;           transient  Set<map.entry<k,v>> entrySet;          transient  int  size;          transient  int  modCount;             int  threshold;          final  float  loadFactor; } 
loadFactor加载因子 
loadFactor加载因子是控制数组存放数据的疏密程度,loadFactor越趋近于1,那么 数组中存放的数据(entry)也就越多,也就越密,也就是会让链表的长度增加,loadFactor越小,也就是趋近于0,数组中存放的数据(entry)也就越少,也就越稀疏。
loadFactor太大导致查找元素效率低,太小导致数组的利用率低,存放的数据会很分散。loadFactor的默认值为0.75f是官方给出的一个比较好的临界值 。
给定的默认容量为 16,负载因子为 0.75。Map 在使用过程中不断的往里面存放数据,当数量达到了 16 * 0.75 = 12 就需要将当前 16 的容量进行扩容,而扩容这个过程涉及到 rehash、复制数据等操作,所以非常消耗性能。
threshold 
threshold = capacity * loadFactor ,当Size>=threshold 的时候,那么就要考虑对数组的扩增了,也就是说,这个的意思就是 衡量数组是否需要扩增的一个标准 。
当桶(bucket)上的结点数大于这个值时会转成红黑树 static final int TREEIFY_THRESHOLD = 8;
当桶(bucket)上的结点数小于这个值时树转链表 static final int UNTREEIFY_THRESHOLD = 6;
只所以不是小于8而是小于6转就是为了防止出现反复转换底层结构的情况出现。 
构造方法 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 public  HashMap ()      this .loadFactor = DEFAULT_LOAD_FACTOR;   }      public  HashMap (Map<? extends K, ? extends V> m)        this .loadFactor = DEFAULT_LOAD_FACTOR;      putMapEntries(m, false );  }      public  HashMap (int  initialCapacity)        this (initialCapacity, DEFAULT_LOAD_FACTOR);  }      public  HashMap (int  initialCapacity, float  loadFactor)        if  (initialCapacity < 0 )          throw  new  IllegalArgumentException("Illegal initial capacity: "  + initialCapacity);      if  (initialCapacity > MAXIMUM_CAPACITY)          initialCapacity = MAXIMUM_CAPACITY;      if  (loadFactor <= 0  || Float.isNaN(loadFactor))          throw  new  IllegalArgumentException("Illegal load factor: "  + loadFactor);      this .loadFactor = loadFactor;      this .threshold = tableSizeFor(initialCapacity);  } 
这里使用默认构造器实例化对象this.loadFactor = DEFAULT_LOAD_FACTOR;把loadFactor设置为0.75。
putVal() JDK1.8版本的HashMap的向里面的添加元素的put()方法实际调用的就是putVal()方法。
1 2 3 public  V put (K key, V value)      return  putVal(hash(key), key, value, false , true ); } 
不过先使用了hash()方法得到了key的hash值,这个hash()方法相比JDK7,简单了一点(因为后面用到了红黑树保证效率)
1 2 3 4 static  final  int  hash (Object key)      int  h;     return  (key == null ) ? 0  : (h = key.hashCode()) ^ (h >>> 16 ); } 
执行put操作的时候才真正构建table数组 
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 final  V putVal (int  hash, K key, V value, boolean  onlyIfAbsent,                    boolean  evict)      Node<K,V>[] tab; Node<K,V> p; int  n, i;          if  ((tab = table) == null  || (n = tab.length) == 0 )         n = (tab = resize()).length;          if  ((p = tab[i = (n - 1 ) & hash]) == null )         tab[i] = newNode(hash, key, value, null );          else  {         Node<K,V> e; K k;                  if  (p.hash == hash &&             ((k = p.key) == key || (key != null  && key.equals(k))))                                  e = p;                  else  if  (p instanceof  TreeNode)                          e = ((TreeNode<K,V>)p).putTreeVal(this , tab, hash, key, value);                  else  {                          for  (int  binCount = 0 ; ; ++binCount) {                                  if  ((e = p.next) == null ) {                                          p.next = newNode(hash, key, value, null );                                          if  (binCount >= TREEIFY_THRESHOLD - 1 )                          treeifyBin(tab, hash);                                          break ;                 }                                  if  (e.hash == hash &&                     ((k = e.key) == key || (key != null  && key.equals(k))))                                          break ;                                  p = e;             }         }                  if  (e != null ) {                           V oldValue = e.value;                          if  (!onlyIfAbsent || oldValue == null )                                  e.value = value;                          afterNodeAccess(e);                          return  oldValue;         }     }          ++modCount;          if  (++size > threshold)         resize();          afterNodeInsertion(evict);     return  null ; }  
  if ((tab = table) == null || (n = tab.length) == 0)table未初始化或者长度为0,进行扩容。n = (tab = resize()).length; resize方法即是初始化方法,也是扩容方法。 
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 final  Node<K,V>[] resize() {    Node<K,V>[] oldTab = table;     int  oldCap = (oldTab == null ) ? 0  : oldTab.length;     int  oldThr = threshold;     int  newCap, newThr = 0 ;     if  (oldCap > 0 ) {                  if  (oldCap >= MAXIMUM_CAPACITY) {             threshold = Integer.MAX_VALUE;             return  oldTab;         }                  else  if  ((newCap = oldCap << 1 ) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY)             newThr = oldThr << 1 ;      }     else  if  (oldThr > 0 )          newCap = oldThr;     else  {                   newCap = DEFAULT_INITIAL_CAPACITY;         newThr = (int )(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);     }          if  (newThr == 0 ) {         float  ft = (float )newCap * loadFactor;         newThr = (newCap < MAXIMUM_CAPACITY && ft < (float )MAXIMUM_CAPACITY ? (int )ft : Integer.MAX_VALUE);     }     threshold = newThr;     @SuppressWarnings({"rawtypes","unchecked"})          Node<K,V>[] newTab = (Node<K,V>[])new  Node[newCap];     table = newTab;     if  (oldTab != null ) {                  for  (int  j = 0 ; j < oldCap; ++j) {             Node<K,V> e;             if  ((e = oldTab[j]) != null ) {                 oldTab[j] = null ;                 if  (e.next == null )                     newTab[e.hash & (newCap - 1 )] = e;                 else  if  (e instanceof  TreeNode)                     ((TreeNode<K,V>)e).split(this , newTab, j, oldCap);                 else  {                      Node<K,V> loHead = null , loTail = null ;                     Node<K,V> hiHead = null , hiTail = null ;                     Node<K,V> next;                     do  {                         next = e.next;                                                  if  ((e.hash & oldCap) == 0 ) {                             if  (loTail == null )                                 loHead = e;                             else                                  loTail.next = e;                             loTail = e;                         }                                                  else  {                             if  (hiTail == null )                                 hiHead = e;                             else                                  hiTail.next = e;                             hiTail = e;                         }                     } while  ((e = next) != null );                                          if  (loTail != null ) {                         loTail.next = null ;                         newTab[j] = loHead;                     }                                          if  (hiTail != null ) {                         hiTail.next = null ;                         newTab[j + oldCap] = hiHead;                     }                 }             }         }     }     return  newTab; } 
此时我们符合的是这个条件
1 2 3 4 5 6 else  {          newCap = DEFAULT_INITIAL_CAPACITY;     newThr = (int )(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY); } 
1 2 3 4 threshold = newThr; @SuppressWarnings({"rawtypes","unchecked"}) Node<K,V>[] newTab = (Node<K,V>[])new  Node[newCap]; table = newTab; 
此时才完成对存储数组的初始化,大小为16,阈值为12。 
1 2 3 if  ((p = tab[i = (n - 1 ) & hash]) == null )      tab[i] = newNode(hash, key, value, null ); 
if ((p = tab[i = (n - 1) & hash]) == null)只要经过这个判断p就被赋值了,该位置数组元素为空时,p为null。只要不为空就是链表头或者树根节点。
键相等情况,都是覆盖值,返回旧值 。
1 2 3 4 5 Node<K,V> e; K k; if  (p.hash == hash &&    ((k = p.key) == key || (key != null  && key.equals(k))))     e = p; 
1 2 3 4 else  if  (p instanceof  TreeNode)         e = ((TreeNode<K,V>)p).putTreeVal(this , tab, hash, key, value); 
使用的是尾插法插入 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 else  {         for  (int  binCount = 0 ; ; ++binCount) {                  if  ((e = p.next) == null ) {                          p.next = newNode(hash, key, value, null );                          if  (binCount >= TREEIFY_THRESHOLD - 1 )                  treeifyBin(tab, hash);                          break ;         }                  if  (e.hash == hash &&             ((k = e.key) == key || (key != null  && key.equals(k))))                          break ;                  p = e;     } } 
1 2 3 4 5 6 7 8 9 if  (e != null ) {     V oldValue = e.value;     if  (!onlyIfAbsent || oldValue == null )                  e.value = value;     afterNodeAccess(e);          return  oldValue; } 
1 2 3 4 5 6   if  (++size > threshold)       resize();      afterNodeInsertion(evict);   return  null ; 
当HashMap元素大于阈值12时调用resize()。阈值,数组长度扩大为2倍。
1 2 3 else  if  ((newCap = oldCap << 1 ) < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY)    newThr = oldThr << 1 ;  
①如果定位到的数组位置没有元素 就直接插入。 
②如果定位到的数组位置有元素就和要插入的key比较,如果key相同就直接覆盖,如果key不相同,就判断p是否是一个树节点,如果是就调用e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value)将元素添加进入。如果不是就遍历链表插入(插入的是链表尾部)。 
 
treeifyBin() 1 2 3 if  (binCount >= TREEIFY_THRESHOLD - 1 )  treeifyBin(tab, hash); 
当我们的链表节点大于阈值8点时候,调用treeifyBin()方法,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 final  void  treeifyBin (Node<K,V>[] tab, int  hash)      int  n, index; Node<K,V> e;     if  (tab == null  || (n = tab.length) < MIN_TREEIFY_CAPACITY)         resize();     else  if  ((e = tab[index = (n - 1 ) & hash]) != null ) {         TreeNode<K,V> hd = null , tl = null ;         do  {             TreeNode<K,V> p = replacementTreeNode(e, null );             if  (tl == null )                 hd = p;             else  {                 p.prev = tl;                 tl.next = p;             }             tl = p;         } while  ((e = e.next) != null );         if  ((tab[index] = hd) != null )             hd.treeify(tab);     } } 
if (tab == null || (n = tab.length) < MIN_TREEIFY_CAPACITY)当我们链表长度为8的时候也不一定树化,而是要判断数组长度是否要大于static final int MIN_TREEIFY_CAPACITY = 64;如果不满足则会扩容。也就是树化的条件有两个
get() 1 2 3 4 public  V get (Object key)      Node<K,V> e;     return  (e = getNode(hash(key), key)) == null  ? null  : e.value; } 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 final  Node<K,V> getNode (int  hash, Object key)      Node<K,V>[] tab; Node<K,V> first, e; int  n; K k;     if  ((tab = table) != null  && (n = tab.length) > 0  &&         (first = tab[(n - 1 ) & hash]) != null ) {                  if  (first.hash == hash &&              ((k = first.key) == key || (key != null  && key.equals(k))))             return  first;                  if  ((e = first.next) != null ) {                          if  (first instanceof  TreeNode)                 return  ((TreeNode<K,V>)first).getTreeNode(hash, key);                          do  {                 if  (e.hash == hash &&                     ((k = e.key) == key || (key != null  && key.equals(k))))                     return  e;             } while  ((e = e.next) != null );         }     }     return  null ; } 
e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))) 前面比较的是hash值,后面比较的是对象是否相等。(==相等,或者equals相等)。