hashMap源码分析

Posted youxin2012

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hashMap源码分析相关的知识,希望对你有一定的参考价值。

以下hashMap源码为java 7

1. 存储结构

结点Entry

static class Entry<K,V> implements Map.Entry<K,V> 
        final K key;
        V value;
        Entry<K,V> next;
        int hash;

        /**
         * Creates new entry.
         */
        Entry(int h, K k, V v, Entry<K,V> n) 
            value = v;
            next = n;
            key = k;
            hash = h;
        
 ...

2. 数据结构:表+链

其中hash(key) 决定 结点构成的“链”在“表” 中的索引。

indexForKey(key1) = x
hash(key1) == hash(key2)
则key1,key2的k-v队插入table中索引为x的元素构成的链表中。

3. 插入,删除,查询源码实现

插入操作put

  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);  //计算key在table中的索引值
        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)))  //key值已存在,则覆盖value,并返回原value
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            
        

        modCount++;
        addEntry(hash, key, value, i);  //key不存在,则插入该key-value元素(Entry)
        return null;
    

     //检查table是否需要扩展,并在在table索引为bucketIndex的链表中插入k-v元素Entry(hash, key, value)
    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);
    

    //在table索引为bucketIndex的链表中插入k-v元素Entry(hash, key, value)
    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++;
    

图示插入过程:待补

查询操作get
查询过程 同 插入操作过程中查看key是否存在:
key-> indexForKey(key) 确定在table中的索引,在该索引处的链表中查找并确定。

   public V get(Object key) 
        if (key == null)
            return getForNullKey();
        Entry<K,V> entry = getEntry(key);

        return null == entry ? null : entry.getValue();
    

    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;
    

note: hashMap中key与value都可以为null, 在插入过程中 hash(key)=0 ,存在于table[0]处的链表中。

删除remove
参考插入过程,删除采用类似方法是先不难理解,不做展开。

对此种hash算法产生质疑,hashMap对String 提供可变哈希算法( alternative hashing)可以在系统参数中设置,降低String 哈希冲撞的可能性(ALTERNATIVE_HASHING_THRESHOLD)

4. hashMap与hashTable的对比

相似点:均存储key-value结构
不同点:

  • hashMap为非线程安全,hashTable为线程安全(方法内synchronize)
  • hashMap 中key,value 均可以为null, hashTable均不可以
  • 扩展性 LinkedHashMap 继承hashMap,可以轻松将hashMap替换为LinkedHashMap 用以满足 需要保持插入顺序的key-value

以上是关于hashMap源码分析的主要内容,如果未能解决你的问题,请参考以下文章

从Java源码的角度来分析HashMap与HashTable的区别

JDK1.7&1.8源码对比分析集合HashMap

Java集合源码学习笔记HashMap分析

源码分析TreeMap和TreeSet

HashMap实现原理分析

HashMap实现原理分析