Java集合之HashMap源码解析

HashMap 

非线程安全的不可重复元素的键值对

继承AbstractMap,为Map的骨架实现;

实现了Cloneable,实现浅克隆;

实现了序列化接口,并自定义了readObject、writeObject方法

 

采用哈希表实现,平均查找的时间复杂度为O(1+α/2)  α为加载因子,其中冲突解决的处理在1.8版本有升级

JDK1.6版本的HashMap采用数组+链表的方式实现,如果hash函数不均匀,最大的查找时间复杂度为O(1+l) l为链表长度(同一个桶中元素数量)

JDK1.8版本的HashMap采用数组+链表or红黑树的方式实现,优化了hash函数不均匀时的最大查找时间复杂度为O(1+log2l)

 

下面对JDK1.6版本的代码重点方法进行分析,JDK1.8与1.6的主要区别是判断链表长度超过预设值(默认为8),需要将链表转换为红黑树,可以阅读美团技术团队的重新认识HashMap一文。

 

重点方法

1、数据结构及构造器

(1)元素定义

    static class Entry<K,V> implements Map.Entry<K,V> {//内部静态类定义Map元素
        final K key;
        V value;
        Entry<K,V> next;//指向链表下一个元素
        final int hash;//hash码

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

        public final K getKey() {
            return key;
        }

        public final V getValue() {
            return value;
        }

        public final V setValue(V newValue) {
            V oldValue = value;
            value = newValue;
            return oldValue;
        }

        public final boolean equals(Object o) {//重写equal方法
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry e = (Map.Entry)o;
            Object k1 = getKey();
            Object k2 = e.getKey();
            if (k1 == k2 || (k1 != null && k1.equals(k2))) {
                Object v1 = getValue();
                Object v2 = e.getValue();
                if (v1 == v2 || (v1 != null && v1.equals(v2)))
                    return true;//key、value均相等时返回true
            }
            return false;
        }

        public final int hashCode() {
            return (key==null   ? 0 : key.hashCode()) ^
                   (value==null ? 0 : value.hashCode());//key、value的位异或
        }

        public final String toString() {
            return getKey() + "=" + getValue();
        }

        /**
         * This method is invoked whenever the value in an entry is
         * overwritten by an invocation of put(k,v) for a key k that's already
         * in the HashMap.
         */
        void recordAccess(HashMap<K,V> m) {//当put覆盖原始数据时执行(供子类使用)
        }

        /**
         * This method is invoked whenever the entry is
         * removed from the table.
         */
        void recordRemoval(HashMap<K,V> m) {//当entry被删除时执行(供子类使用)
        }
    }

(2)构造器,提供了无参、给定数组长度、给定装填因子及拷贝构造器

    static final int DEFAULT_INITIAL_CAPACITY = 16;//默认容量

    static final int MAXIMUM_CAPACITY = 1 << 30;//最大容量

    static final float DEFAULT_LOAD_FACTOR = 0.75f;//默认装载因子

    transient Entry[] table;//哈希表数组

    transient int size;//元素个数

    int threshold;//阈值;元素最大个数

    final float loadFactor;//哈希表的装填因子(表中填入的记录数/哈希表的长度)

    transient volatile int modCount;//fail-fast迭代器的修改判断(与List类似)

    public HashMap(int initialCapacity, float loadFactor) {//按给定的哈希表长度及装填因子构造HashMap
        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);

        // Find a power of 2 >= initialCapacity 找到大于给定的哈希表长度的最小2的次幂作为实际容量
        int capacity = 1;
        while (capacity < initialCapacity)
            capacity <<= 1;

        this.loadFactor = loadFactor;//设置装填因子
        threshold = (int)(capacity * loadFactor);//设置阈值为实际容量*装填因子
        table = new Entry[capacity];//新建大小为实际容量的哈希表数组
        init();
    }

    public HashMap(int initialCapacity) {//按给定的哈希表长度构造HashMap
        this(initialCapacity, DEFAULT_LOAD_FACTOR);
    }

    public HashMap() {//默认构造,按默认参数进行构造
        this.loadFactor = DEFAULT_LOAD_FACTOR;
        threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
        table = new Entry[DEFAULT_INITIAL_CAPACITY];
        init();
    }
    public HashMap(Map<? extends K, ? extends V> m) {//拷贝构造器,支持子类
        this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
                      DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);//按默认的装填因子建立新的HashMap
        putAllForCreate(m);//装填数据
    }

    private void putAllForCreate(Map<? extends K, ? extends V> m) {//拷贝构造器/克隆时的批量写入方法
        for (Iterator<? extends Map.Entry<? extends K, ? extends V>> i = m.entrySet().iterator(); i.hasNext(); ) {//按原始Map的entrySet迭代器进行迭代
            Map.Entry<? extends K, ? extends V> e = i.next();
            putForCreate(e.getKey(), e.getValue());//重新散列并装入Map
        }
    }

(3)插入或覆盖;构造中的putForCreate与其类似只是分开不同方法(JDK1.8里面是合并处理的)

重点为hash值的补充散列及hash桶index的取值处理;与ArrayList类似,也需要对数组进行扩容

    public V put(K key, V value) {//加入Map或重置value,如果为重置返回原始value
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key.hashCode());//取hash值
        int i = indexFor(hash, table.length);//取数组index
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {//循环数组index对应的链表
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {//如果已有数据
                V oldValue = e.value;
                e.value = value;//重置value
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount++;
        addEntry(hash, key, value, i);//新增元素
        return null;
    }

    private V putForNullKey(V value) {//key为null时的插入或重置,如果为重置返回原始的value
        for (Entry
  
  
   
    e = table[0]; e != null; e = e.next) {//查找初始位置的链表
            if (e.key == null) {
                V oldValue = e.value;
                e.value = value;//重置value值
                e.recordAccess(this);//执行该对象的覆盖后操作
                return oldValue;
            }
        }
        modCount++;
        addEntry(0, null, value, 0);//未找到时,进行新增
        return null;
    }

  
  
    static int hash(int h) {//对原始hashCode做补充散列处理(防止原hashCode质量差导致容易引起冲突)
        // This function ensures that hashCodes that differ only by
        // constant multiples at each bit position have a bounded
        // number of collisions (approximately 8 at default load factor).
        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }

    static int indexFor(int h, int length) {//根据hashCode、数组容量返回index
        return h & (length-1);//按位与运算,相当于用hash值对length取模
    }

    void addEntry(int hash, K key, V value, int bucketIndex) {//按hash码、key、value、散列桶index(数组index)新增元素
        Entry<K,V> e = table[bucketIndex];//取原始数组元素
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);//数组元素设置为新增元素,其下一个元素为原始数组元素
        if (size++ >= threshold)//增加元素个数,如果超过阈值,进行扩容操作
            resize(2 * table.length);
    }
    void resize(int newCapacity) {//按指定容量值扩充哈希表数组长度
        Entry[] oldTable = table;
        int oldCapacity = oldTable.length;
        if (oldCapacity == MAXIMUM_CAPACITY) {//如果已经达到最大容量,将阈值置为Integer.MAX_VALUE,避免扩容
            threshold = Integer.MAX_VALUE;
            return;
        }

        Entry[] newTable = new Entry[newCapacity];//按指定容量值建立新的哈希表数组
        transfer(newTable);//将原表数据移动到新表中
        table = newTable;
        threshold = (int)(newCapacity * loadFactor);//设置阈值为指定容量值*装载因子
    }

(4)删除处理

    public V remove(Object key) {//删除元素并返回原始value
        Entry<K,V> e = removeEntryForKey(key);
        return (e == null ? null : e.value);
    }

    final Entry<K,V> removeEntryForKey(Object key) {//根据key值删除元素,如果找不到返回null
        int hash = (key == null) ? 0 : hash(key.hashCode());
        int i = indexFor(hash, table.length);
        Entry<K,V> prev = table[i];//数组index中元素
        Entry<K,V> e = prev;

        while (e != null) {//循环查找index对应链表
            Entry<K,V> next = e.next;
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k)))) {//找到进行删除后返回
                modCount++;
                size--;
                if (prev == e)//该元素在数组中(只有第一次循环相等),将数组元素指向next
                    table[i] = next;
                else
                    prev.next = next;//不在数组中,将上一元素的next指向next
                e.recordRemoval(this);//执行entry删除操作
                return e;
            }
            prev = e;//上一个元素指向当前元素
            e = next;//当前元素指向下一个元素
        }

        return e;
    }

(5)查询处理

    public V get(Object key) {//按key值查找value
        if (key == null)
            return getForNullKey();
        int hash = hash(key.hashCode());//取得hash值
        for (Entry<K,V> e = table[indexFor(hash, table.length)];//找到哈希表数组index
             e != null;
             e = e.next) {//循环查找该哈希值对应的链表数据
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k)))//key为同一对象或相等(提高判断效率)
                return e.value;
        }
        return null;
    }

2、迭代器

    void createEntry(int hash, K key, V value, int bucketIndex) {//按hash码、key、value、散列桶index(数组index)新增元素;不进行扩容操作;在克隆/序列化时使用
        Entry<K,V> e = table[bucketIndex];
        table[bucketIndex] = new Entry<K,V>(hash, key, value, e);
        size++;
    }

    private abstract class HashIterator<E> implements Iterator<E> {//抽象的hash迭代器
        Entry<K,V> next;    // next entry to return
        int expectedModCount;    // For fast-fail
        int index;        // current slot
        Entry<K,V> current;    // current entry

        HashIterator() {
            expectedModCount = modCount;
            if (size > 0) { // advance to first entry
                Entry[] t = table;
                while (index < t.length && (next = t[index++]) == null)//将数组中第一个有值的index设置为next
                    ;
            }
        }

        public final boolean hasNext() {//不可重写方法:判断是否有next
            return next != null;
        }

        final Entry<K,V> nextEntry() {//不可重写方法:取下一元素,没有时抛异常
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            Entry<K,V> e = next;
            if (e == null)
                throw new NoSuchElementException();

            if ((next = e.next) == null) {//链表无下一个
                Entry[] t = table;
                while (index < t.length && (next = t[index++]) == null)//取数组中下一个有值的index设置为next
                    ;
            }
            current = e;//当前元素设置为next并返回
            return e;
        }

        public void remove() {//删除方法,删除当前元素
            if (current == null)
                throw new IllegalStateException();//没有时抛异常
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
            Object k = current.key;
            current = null;//清空current
            HashMap.this.removeEntryForKey(k);//调用根据key值删除元素方法
            expectedModCount = modCount;
        }

    }

    private final class ValueIterator extends HashIterator<V> {//value迭代器
        public V next() {
            return nextEntry().value;
        }
    }

    private final class KeyIterator extends HashIterator<K> {//key迭代器
        public K next() {
            return nextEntry().getKey();
        }
    }

    private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {//entry迭代器
        public Map.Entry<K,V> next() {
            return nextEntry();
        }
    }

    // Subclass overrides these to alter behavior of views' iterator() method
    Iterator<K> newKeyIterator()   {
        return new KeyIterator();
    }
    Iterator<V> newValueIterator()   {
        return new ValueIterator();
    }
    Iterator<Map.Entry<K,V>> newEntryIterator()   {
        return new EntryIterator();
    }

3、作为父类的模板模式设计

在HashMap的代码中,有很多供给子类覆盖的钩子方法,模板方法中规定了其调用的位置。

例如:

init()在构造/克隆/序列化时调用

recordAccess(this)在元素覆盖时调用

recordRemoval(this)在元素删除时调用

newKeyIterator、newValueIterator、newEntryIterator在keySet、Values、EntrySet构造时调用(final)

 

相关的类

HashSet:依托于HashMap的Set。

    private transient HashMap<E,Object> map;//依托于HashMap的数据结构

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();//写入Map的value值

LinkedHashMap:HashMap的子类,增加了按插入顺序或者访问顺序排列的双向链表(链表部分的数据结构与LinkedList类似,并且在JDK1.7版本同时进行了升级)

LinkedHashSet:依托于LinkedHashMap的Set

Hashtable:线程安全的哈希表(比较旧了不建议使用)

IdentityHashMap:hash使用System.identityHashCode计算,可以允许key值重复

WeakHashMap:使用弱引用保存键

    private final ReferenceQueue<K> queue = new ReferenceQueue<K>();

    private static class Entry<K,V> extends WeakReference<K> implements Map.Entry<K,V>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值