HashMap 和 HashTable 的区别





HashMap 和 HashTable 的区别
1、HashMap 是非线程安全的,HashTable 是线程安全的。
2、HashMap 的键和值都允许有 null 值存在,而 HashTable 则不行。
3、因为线程安全的问题,HashMap 效率比 HashTable 的要高。

HashMap put 方法的内部存储结构(jdk 1.7)
1、HashMap<String, Object> hashMap = new HashMap<String, Object>(); 的时候会通过构造函数初始化 map 的大小和扩容因子。
public HashMap() {
    this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR ); // DEFAULT_INITIAL_CAPACITY 初始化大小 16, DEFAULT_LOAD_FACTOR 扩容因子 0.75
}

public HashMap(int initialCapacity , float loadFactor) {
    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);

    this.loadFactor = loadFactor;
    threshold = initialCapacity;
    init();
}

2、map 的初始化大小和扩容因子设置完成之后,map 准备就绪,调用 map.put() 方法向 map 中添加元素。在 put 方法中,如果当前 table(也就是 entry 数组),就在 inflateTable 方法中初始化 table,

public V put(K key, V value ) {
    if (table == EMPTY_TABLE) {
        inflateTable( threshold);
    }
    ...    
    return null;
}

3、在 inflateTable 方法中通过 roundUpToPowerOf2 方法计算当前要初始化的大小是否大于等于 MAXIMUM_CAPACITY(2的 30次方),如果大于等于就设置成 MAXIMUM_CAPACITY 的值,不大于就调用 Integer.highestOneBit 方法计算初始化的大小。
highestOneBit 方法简单的说就是:
3.1、如果一个数是0, 则返回0;
3.2、如果是负数, 则返回 -2147483648;
3.3、如果是正数, 返回的则是跟它最靠近的比它小的2的N次方;例如:17 则返回 16;15 则返回 8;
private void inflateTable(int toSize) {
    // Find a power of 2 >= toSize
    int capacity = roundUpToPowerOf2(toSize);

    threshold = (int) Math.min(capacity * loadFactor, MAXIMUM_CAPACITY  + 1);
    table = new Entry[capacity];
    initHashSeedAsNeeded(capacity);
}

private static int roundUpToPowerOf2( int number ) {
    // assert number >= 0 : "number must be non-negative";
    return number >= MAXIMUM_CAPACITY
            ? MAXIMUM_CAPACITY
            : ( number > 1) ? Integer.highestOneBit(( number - 1) << 1) : 1;
}

4、初始化完成之后再回到 put 方法中,往下看会看到判断 key 值是否是空值,当 key 是空值的时候,调用 putForNullKey 处理。首先取出下标为0的元素,如果当前元素是空元素,就将当前key的 hashcode 设置为0,放在 table 下标为 0 的位置。

public V put(K key, V value) {
    ...
    if (key == null)
        return putForNullKey(value);
    ...
    return null;
}

private V putForNullKey(V value) {
    for (Entry<K,V> e = table[0]; e != null; e = e.next) {
        if (e.key == null) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue ;
        }
    }
    modCount++;
    addEntry(0, null, value , 0);
    return null;
}

5、如果不是空的key,先计算 key 的 hash 值,然后调用 indexFor 方法根据 hash 值和 table 的长度计算出当前的 key 存储在 table 中的位置 i 。计算完位置之后,再取出位置 i 处是否已经有数据,如果有数据并且数据中也有相同的 hash key 值的时候,就将当前的值替换掉旧的值并返回旧的值。循环完之后没有相同的 hash key 值,就调用 addEntry 方法向 table 位置 i 处添加元素。添加规则是将当前 key 存放在位置 i 处链表的最顶部。

public V put(K key, V value) {
    ....
    int hash = hash(key);
    int i = indexFor(hash , table. length);
    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))) {
            V oldValue = e.value;
            e.value = value;
            e.recordAccess(this);
            return oldValue ;
        }
    }

    modCount++;
    addEntry(hash, key , value, i);
    return null;
}

6、添加的时候判断当前位置 i 是否已经大于要扩容的极限(总长度 X 扩容因子),扩容是当前 table 大小的 2 倍。扩容之后重新"散列掩码值 ",然后循环旧 table,将每一个元素都重新计算存储在扩容之后新 table 中的哪个位置上。转换完之后,再计算下次要扩容的极限大小是多少。

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);
}

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, initHashSeedAsNeeded(newCapacity));
    table = newTable ;
    threshold = (int)Math.min(newCapacity * loadFactor, MAXIMUM_CAPACITY + 1);
}

void transfer(Entry[] newTable, boolean rehash) {
    int newCapacity = newTable.length;
    for (Entry<K,V> e : table) {
        while(null != e) {
            Entry<K,V> next = e.next;
            if (rehash) {
                e.hash = null == e.key ? 0 : hash(e.key);
            }
            int i = indexFor (e.hash, newCapacity);
            e.next = newTable[i];
            newTable[i] = e;
            e = next ;
        }
    }
}

HashMap get 方法的内部存储结构(jdk 1.7)
1、get 方法比较简单,进来之后先判断是否 key 是否是 null,是 null 就特殊处理,不是就常规处理并返回这个 key 对应的值。V 是泛型,声明 HashMap 的时候中的 value 类型。
public V get(Object key) {
    if (key == null)
        return getForNullKey();
    Entry<K,V> entry = getEntry(key );

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

2、当 key 是 null  的情况,先判断当前 table 的 size 是否大于 0,大于 0 就继续,否则返回 null。然后再直接获取 hashMap 中 table(Entry) 下标为 0 的位置的链表,循环这个链表,依次判断每个元素的 key 是否为 null,为 null 就返回对应的值。

private V getForNullKey() {
    if (size == 0) {
        return null;
    }
    for (Entry<K,V> e = table[0]; e != null; e = e.next) {
        if (e.key == null)
            return e.value;
    }
    return null;
}

3、key 不为 null 的情况,同样是先判断当前 table 的 size 是否大于 0,大于 0 就继续,否则返回 null。然后再计算要查找 key 的 hash 码,通过 hash 码和 table 的长度计算出这个 key 对应 table 的下标,得到下标之后取出 hashMap 中对应下标的 table(Entry) 链表,循环这个链表,依次判断每个元素的 key 是否与当前 key 相同 并且 hash 码也相同,相同就返回对应的值,不相同就返回 null。

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;
}









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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值