user961627
user961627

Reputation: 12747

copying a java treemap of treemap using putAll()

I have a TreeMap within a TreeMap.

TreeMap <String, TreeMap<String, Double>> x_probs_org = new TreeMap<String, TreeMap<String, Double>>();

But when I make another one with exactly the same definition, and then copy the first one:

x_probs.putAll(x_probs_org);

I notice the new treemap doesn't copy everything. It copies all the String keys correctly, but only the last element in the value (TreeMap). Is there any easier way to do this right, apart from scrolling through the entire first treemap and then adding the elements to the new one?

I just need to have identical data structures with identical data to begin with. What I did was to run the loop through which I populated the first treemap, and then simply put the next one with it, in the same loop. This didn't work either:

// build tempMap up there...
x_probs_org.put(tokens[0], tempMap);    
x_probs.put(tokens[0], tempMap);

x_probs insists on missing data that x_probs_org manages to get. Does "tempMap" get exhausted by populating something once?

Upvotes: 4

Views: 11024

Answers (4)

Mikko Maunu
Mikko Maunu

Reputation: 42084

Most likely you are reusing references (like tempMap) and that's why also your x_probs_org is not what you expect it to be. Or you get surprised because you modify elements of copy via original.

Following works perfectly:

TreeMap<String, TreeMap<String, Double>> x_probs_org = 
    new TreeMap<String, TreeMap<String, Double>>();
TreeMap<String, Double> inner = new  TreeMap<String, Double>();
inner.put("entry1", 1d);
inner.put("entry2", 2d);
x_probs_org.put("inner", inner);

TreeMap<String, TreeMap<String, Double>> x_probs = 
    new TreeMap<String, TreeMap<String, Double>>();
x_probs.putAll(x_probs_org);

Upvotes: 0

Francisco Paulo
Francisco Paulo

Reputation: 6322

You can simply use the TreeMap(SortedMap<K,? extends V> m) constructor, like this:

TreeMap <String, TreeMap<String, Double>> x_probs_org = new TreeMap<String, TreeMap<String, Double>>();

TreeMap <String, TreeMap<String, Double>> x_probs = new TreeMap<String, TreeMap<String, Double>>(x_probs_org);

Upvotes: 0

codeisee
codeisee

Reputation: 1732

Another Easy Way as following:

newMap = x_probs_org.clone();

Upvotes: 0

assylias
assylias

Reputation: 328598

This works for me:

public static void main(String[] args) {
    Map <String, Map<String, Double>> map = new TreeMap<String, Map<String, Double>>();
    Map<String, Double> innerMap = new TreeMap<String, Double>();
    innerMap.put("a", 1.0);
    innerMap.put("b", 2.0);
    map.put("inner1", innerMap);
    innerMap = new TreeMap<String, Double>();
    innerMap.put("c", 3.0);
    innerMap.put("d", 4.0);
    map.put("inner2", innerMap);

    Map <String, Map<String, Double>> newMap = new TreeMap<String, Map<String, Double>>();
    newMap.putAll(map);

    System.out.println(map); // prints {inner1={a=1.0, b=2.0}, inner2={c=3.0, d=4.0}}
    System.out.println(newMap); // prints {inner1={a=1.0, b=2.0}, inner2={c=3.0, d=4.0}}
}

Upvotes: 4

Related Questions