HashMap的简单使用之putAll

时间:2018-11-13
本文章向大家介绍HashMap的putAll方法介绍说明 ,需要的朋友可以参考一下

jdk1.8

使用putAll时,新map中的值仅为旧map值所对应对象的引用,并不会产生新对象。

如下,使用for循环赋值!

public void putAll(Map<? extends K, ? extends V> m) {
    putMapEntries(m, true);
}
final void putMapEntries(Map<? extends K, ? extends V> m, boolean evict) {
    int s = m.size();
    if (s > 0) {
        ....
    ....
        for (Map.Entry<? extends K, ? extends V> e : m.entrySet()) {
            K key = e.getKey();
            V value = e.getValue();
            putVal(hash(key), key, value, false, evict);
        }
    }
}