Question

I have the following 2 cases :

Case 1:

    HashMap<String, HashMap<String,String>> mainMap  = new HashMap<String, HashMap<String,String>>();
    
    HashMap <String, String> subMap  = new HashMap<String,String>();
    
    subMap.put("11", "12");
    subMap.put("13", "124");
    subMap.put("21", "122");
    subMap.put("14", "152");
    
    System.out.println("For One : "+subMap);
    mainMap.put("one", subMap);
    
    subMap.put("15", "152");
    subMap.put("17", "152");
    
    System.out.println("For Two : "+subMap);
    mainMap.put("two", subMap);
    
    System.out.println(mainMap); 

I am expecting the following output :

For One : {21=122, 13=124, 14=152, 11=12}
For Two : {21=122, 17=152, 15=152, 13=124, 14=152, 11=12}
{two={21=122, 17=152, 15=152, 13=124, 14=152, 11=12}, one={21=122, 13=124, 14=152, 11=12}}

However, I am getting this output instead :

For One : {21=122, 13=124, 14=152, 11=12}
For Two : {21=122, 17=152, 15=152, 13=124, 14=152, 11=12}
{two={21=122, 17=152, 15=152, 13=124, 14=152, 11=12}, one={21=122, 17=152, 15=152, 13=124, 14=152, 11=12}}

Case 2:

I also tried the following :

HashMap<String,String> map12 = new HashMap<String, String>();
        
map12.put("1","1234");
map12.put("2","1234");
         
System.out.println("map12 : "+map12);
        
map12.put("3","1234");

And I got the correct output which was expected :

map12 : {2=1234, 1=1234}

Why does the code in case1 not give the expected out but case2 works perfectly fine?

Was it helpful?

Solution

The value in your mainMap is a reference to a HashMap object. Any reference to that object (e.g. subMap) can change it.

OTHER TIPS

You are using the same hashmap, you have to create a new instance of it.

HashMap<String, HashMap<String,String>> mainMap  = new HashMap<String, HashMap<String,String>>();

HashMap <String, String> subMap  = new HashMap<String,String>();

subMap.put("11", "12");
subMap.put("13", "124");
subMap.put("21", "122");
subMap.put("14", "152");

System.out.println("For One : "+subMap);
mainMap.put("one", subMap);

subMap  = new HashMap<String,String>(); /// <<<<<<<<
subMap.put("21", "122");
subMap.put("17", "152");
subMap.put("15", "152");
subMap.put("13", "124");
subMap.put("14", "152");
subMap.put("11", "12");

System.out.println("For Two : "+subMap);
mainMap.put("two", subMap);

System.out.println(mainMap); 

If you are confused, this may help:

This line will create a HashMap object, and assigned a subMap reference to it. HashMap subMap = new HashMap();

subMap-------->[hashObject]

This line will add "one" as key, and subMap "reference" as value mainMap.put("one", subMap);

so, mainMap.put("one", subMap---------------> [hashObject]

Then you prints the value assign to key "one" i.e. {21=122, 13=124, 14=152, 11=12}

Now, you are putting values to the same HashMap object [hashObject]

subMap.put("15", "152");

subMap.put("17", "152");

Now, map contains

{21=122, 17=152, 15=152, 13=124, 14=152, 11=12}

And then, you assigned it as:

mainMap.put("two", subMap---------------> [hashObject]

Try this:

System.out.println("For One : "+subMap);    

System.out.println("For Two : "+subMap);

Output:

For One : {11=12, 13=124, 14=152, 15=152, 17=152, 21=122}

For Two : {11=12, 13=124, 14=152, 15=152, 17=152, 21=122}

which is the same HashMap object, no new object is created and both "one" and "two" has value, a reference referring to the same object.

Please note that this is an object not a primitive.

int a = 10;
int b = a;
a++;

output:

a=11

b=10

but in case of object its reference not the object,

Object a = new Object();

Object b = a;

any change done using any of these references will effect the same object, because both are referring the same object.

In your case 2, you have first print the map and then added the next key-value.

HashMap doesn't guarantee a specific order of the items stored inside. It's unordered.

Use LinkedHashMap instead. It stores the items in the order, in which they were inserted.

Read the Java Docs for more Infos:

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html http://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top