读书人

hashMap内部机制之篇2

发布时间: 2013-07-04 11:45:32 作者: rapoo

hashMap内部机制之篇二

上篇浅谈了一下hashMap内部实现的大概模式,现在因为笔者尝试着模拟实现了下hashMap的功能,想来研究源码做个对比,因此在此记录下研究此源码的一点点感悟。

?

1 从put方法谈起。

?

摘录的hashMap中的源码如下:

  public V put(K key, V value) {        if (key == null)            return putForNullKey(value);        int hash = hash(key.hashCode());        int i = indexFor(hash, table.length);        for (Entry<K,V> e = table[i]; e != null; e = e.next) {//遍历数组看是否存在重复的键值,若有重复,则用新的value值代替旧的value            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;    }

?

private V putForNullKey(V value) {//key为空的情况        for (Entry<K,V> e = table[0]; e != null; e = e.next) {//遍历数组,若有key也为空的情况,则用新的value代替老的value//此处注意:hashMap中的键值是可以为空的            if (e.key == null) {                V oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }        modCount++;        addEntry(0, null, value, 0);//若不为空,调用此方法        return null;    }

?

?加入键值对的关键代码:

 void addEntry(int hash, K key, V value, int bucketIndex) {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) {            threshold = Integer.MAX_VALUE;            return;        }        Entry[] newTable = new Entry[newCapacity];        transfer(newTable);        table = newTable;        threshold = (int)(newCapacity * loadFactor);    }    /**     * Transfers all entries from current table to newTable.     */    void transfer(Entry[] newTable) {        Entry[] src = table;        int newCapacity = newTable.length;        for (int j = 0; j < src.length; j++) {            Entry<K,V> e = src[j];            if (e != null) {                src[j] = null;                do {                    Entry<K,V> next = e.next;                    int i = indexFor(e.hash, newCapacity);                    e.next = newTable[i];                    newTable[i] = e;                    e = next;                } while (e != null);            }        }    }

?通过分析源码的扩容方法可知,源码用到了一个临时变量数组src来解决把旧的table数组中的Entry全部重新散列到新数组newTable中,这正是笔者在模拟实现时遇到的问题:即要得到旧table数组中全部的元素,又要散列到一个新数组中,中间无法过度。

?

注:若读者还未明白,可以参看http://blog.csdn.net/mirage520/article/details/6452628;

?

?

读书人网 >编程

热点推荐