HashMap

in 编程 with 0 comment

HashMap

当new HashMap()时,首先虚拟机会先调用ClassLoader中的checkPackageAccess()方法,检查是否有问题

当使用hashMap.put(K key, V value);底层源码是:

public V put (K key,V value){
   return putVal(hash(key),key,value,false,true);
}

hash算法:

static final int hash(Object key){
    int  h;
    return (key==null)? 0:(h=key.hashCode())^(h>>>16);
}

hashCode算法:

@Contract(pure= true)
public int hashCode(){
  int h = hash;
  if(h==0&& value.length>0){
     char[] val = value;
     for(int i=0;i<value.length;i++){
         h = 31*h+val[i];
     }
     hash = h;
  }
  return h;
}


pulVal函数:

 /**
  * Implements Map.put and related methods   //实现了Map.put及相关方法
  * @param hash hash for key                 //参数 hash 用于key的hash
  * @param key the key                       //参数 key  这是key
  * @param value the value to put            //参数 value这是放入的value
  * @param onlyIfAbsent if true, don't change existing value
  //参数onlyIfAbsent 为真,不改变现有的值
  * @param evict if false, the table is in creation mode.
  //参数evict 为假,这个表处于创建模式(mode)
  * @return previous value, or null if none  //返回值 previous值如果没有则为空
  */
    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) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }

get函数:

public V get(Object key){
   Node<K,V> e;
   return (e = getNode(hash(key),key)) == null? null:e.value;
}

getNode函数:

/**
* Implements Map.get and related methods //实现的Map.get及相关方法
* @param hash hash for key               //参数 hash 用于key的hash
* @param key the key                     //参数 key  这是key 
* @return the node, or null if none      //返回值 这个node如果没有则为空
*/
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 && // always check first node
             ((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;
}