0

Problem

If I explicitly loop through every element in hash map A and "put" each element in hash map B then I have no problem. But if try calling A.putAll(B) for some reason B ends up null.

The code that is working is the basic iterator approach show below:

    Iterator it = A.entrySet().iterator();
    while(it.hasNext()){
        Map.Entry entry = (Map.Entry) it.next();
        B.put((Integer)entry.getKey(),(Integer)entry.getKey());
    }

Where I run into trouble is when I do this.

HashMap A = loadHashMapWithData();
HashMap B = new HashMap();
A.putAll(B);
System.out.println(A);
System.out.println(B);

The second hashmap I am trying to pass data into always ends up "null". Going forward I am using the 1st approach but it would be nice to know why putAll is failing.

4

1 回答 1

3

A.putAll(B)正在将所有元素B放入A- 我认为您想要B.putAll(A)将所有元素A放入B

object.method(parameter)呼唤method传递-你想呼唤object,你的也想呼唤parameterputAllBBobject

于 2013-08-03T23:49:06.927 回答