zoukankan      html  css  js  c++  java
  • hashMap源码解析

    hashMap底层有一个数组存放所有的数据:

      Node<K,V>[] tab;//链表数组

    hashMap最重要的两个方法:

    put,根据hash值找到位置,如果没有发生冲突,键值对放入数组中,如果发生冲突,需要解决冲突,如果当前已经存在key的映射关系,新value会替换以前的,如果不存在,会把当前键值对放在找到的索引位置链表末端;

     1 /**
     2      * Implements Map.put and related methods
     3      *
     4      * @param hash 根据key计算出来的hash值
     5      * @param key the key
     6      * @param value the value to put
     7      * @param onlyIfAbsent if true, 不改变已经存在的值
     8      * @param evict if false,表处在创建模式
     9      * @return 之前的值,如果不存在返回null
    10      */
    11     final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
    12                    boolean evict) {
    13         Node<K,V>[] tab; //存放底层数组
    14 
    15        Node<K,V> p; //存放传进来的键值对
    16 
    17     int n, i;
    18 
    19   //如果数组为空,重新生成新的数组
    20         if ((tab = table) == null || (n = tab.length) == 0)
    21             n = (tab = resize()).length;
    22 
    23   //如果当前key的hash值没有发生冲突,把它放在数组的最后一个,生成一个新的结点
    24         if ((p = tab[i = (n - 1) & hash]) == null)
    25             tab[i] = newNode(hash, key, value, null);
    26         else {
    27             Node<K,V> e; 
    28 
    29     K k;
    30 
    31     //如果hash值相同并且key相同
    32             if (p.hash == hash &&
    33                 ((k = p.key) == key || (key != null && key.equals(k))))
    34                 e = p;
    35             else if (p instanceof TreeNode)
    36                 e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
    37             else {
    38 
    39     //hash值相同,key不同,不是TreeNode,需要解决冲突,找到一个位置放置,一般是放在当前结点的链表尾部
    40                 for (int binCount = 0; ; ++binCount) {
    41 
    42       //当找到链表的尾部,生成新的结点存放键值对
    43                     if ((e = p.next) == null) {
    44                         p.next = newNode(hash, key, value, null);
    45                         if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
    46                             treeifyBin(tab, hash);
    47                         break;
    48                     }
    49 
    50        //如果键和hash都相同,说明已经存在映射关系
    51                     if (e.hash == hash &&
    52                         ((k = e.key) == key || (key != null && key.equals(k))))
    53                         break;
    54                     p = e;
    55                 }
    56             }
    57 
    58     //已经存在当前key的映射,需要替换之前的值,
    59             if (e != null) { // existing mapping for key
    60                 V oldValue = e.value;
    61                 if (!onlyIfAbsent || oldValue == null)
    62                     e.value = value;
    63                 afterNodeAccess(e);
    64                 return oldValue;
    65             }
    66         }
    67         ++modCount;
    68         if (++size > threshold)
    69             resize();
    70         afterNodeInsertion(evict);
    71         return null;
    72     } 

     get方法:

     1 //根据指定的键来查找值
     2 
     3 
     4 public V get(Object key) {
     5         Node<K,V> e;
     6         return (e = getNode(hash(key), key)) == null ? null : e.value;
     7     }
     8 
     9 /**
    10      * Implements Map.get and related methods
    11      *
    12      * @param hash hash for key
    13      * @param key the key
    14      * @return the node, or null if none
    15      */
    16     final Node<K,V> getNode(int hash, Object key) {
    17         Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    18 
    19    //如果底层数组不为空,并且可以根据hash找到键值对在数组中的位置
    20         if ((tab = table) != null && (n = tab.length) > 0 &&
    21             (first = tab[(n - 1) & hash]) != null) {
    22 
    23     //总是先检查第一个结点,找到则返回
    24             if (first.hash == hash && // always check first node
    25                 ((k = first.key) == key || (key != null && key.equals(k))))
    26                 return first;
    27             if ((e = first.next) != null) {
    28                 if (first instanceof TreeNode)
    29                     return ((TreeNode<K,V>)first).getTreeNode(hash, key);
    30                 do {
    31 
    32        //循环遍历链表,直到找到结点
    33                     if (e.hash == hash &&
    34                         ((k = e.key) == key || (key != null && key.equals(k))))
    35                         return e;
    36                 } while ((e = e.next) != null);
    37             }
    38         }
    39         return null;
    40     }

     分享从伯乐在线看到的一篇好文章 :

    java8中hashMap的性能提升

  • 相关阅读:
    最小的K个数
    CentOS 7 连接不到网络解决方法
    数组中超过一半的数字
    字符串的排列
    二叉搜索树与双向链表
    复杂链表的复制
    二叉树中和为某一数值的路径
    二叉搜索树的后序遍历序列
    从上到下打印二叉树
    java的图形文档
  • 原文地址:https://www.cnblogs.com/zhhx/p/4933433.html
Copyright © 2011-2022 走看看