简体   繁体   中英

I do not understand my output. Why am I getting the same thing twice?

The following program maintains 2 data structures named:

map of type HashMap
map_1 also of type HashMap

At the beginning map is populated with key : 1 and value : suhail . Then this map is inserted into map_1 with key 20 .

Again the map is populated with key : 1 and another value : CSE . This map is again inserted into map_1 .

import java.util.*;

class KeyTest {
    public static void main(String args[]) {
        Map<Integer,String> map = new HashMap<Integer,String>();

        Map<Integer,Object> map_1 = new HashMap<Integer,Object>();

        map.put(1,"suhail");

        map_1.put(20,map);

        map.put(1,"CSE");

        map_1.put(21,map);

        Set<Integer> keys = map_1.keySet();
        Iterator i = keys.iterator();
        while(i.hasNext()) {
            System.out.println(map_1.get((Integer)i.next()));
        }

    }
}

This is what I get, when I print map_1 values :

{1=CSE}
{1=CSE}

But this is not, what I expected.According to me this is how the program should have been running :

[1,suhail]--> map
[20,[1,suhail]]---> map_1
[1,CSE]--> map (A replace, because of the same keys)
[21,[1,CSE]]--->map_1

So the output should have been :

[1,suhail]
[1,CSE]

Can anyone please explain me, why don't I get this output

When you insert an object map into map_1 , it does not get copied. When you modify it outside the map_1 , the stored object is modified, too, because it's the same object.

Re-assign a new map to map to fix this problem:

Map<Integer,Object> map_1 = new HashMap<Integer,Object>();
Map<Integer,String> map = new HashMap<Integer,String>();

map.put(1,"suhail");
// The first "map" object gets inserted
map_1.put(20,map);

// Make a new object
map = new HashMap<Integer,String>();

map.put(1,"CSE");
// Now the second object gets inserted
map_1.put(21,map);

In a map you can have one value for one key so if you put something again on the same key that will get replaced with the new object. so when you put something again on key:1 the value is replaced. you have to create a new object to bypass that behavior means new map.

and you have put map ref in the map1 so when you edit it its inner copy is also modified.

That's because you're overwriting the last value in the same Map instance.

map <- [1: "suhail"]
map_1 <- [20: [1: "suhail"]]

map <- 1, 'CSE' (you lost "suhail", overwriting key 1) [1: "CSE"]
map_1 <- 21, map, same object in two different keys. [20: [1: "CSE"], 21: [1: "CSE"]]

In that case you have the same instance in related to two different keys in map_1 .

To do what you want, you have to create a new Map instance for map .

import java.util.*;

class KeyTest {
    public static void main(String args[]) {
        Map<Integer,String> map = new HashMap<Integer,String>();

        Map<Integer,Object> map_1 = new HashMap<Integer,Object>();

        map.put(1,"suhail");

        map_1.put(20,map);

        map = new HashMap<Integer,String>();
        map.put(1,"CSE");

        map_1.put(21,map);

        Set<Integer> keys = map_1.keySet();
        Iterator i = keys.iterator();
        while(i.hasNext()) {
            System.out.println(map_1.get((Integer)i.next()));
        }

    }
}

The output is correct, in the below statement

map.put(1,"suhail"); 
//--> map has [1 = suhail]
map_1.put(20,map);  
//--> map1 has [20 = [1 = suhail]]
map.put(1,"CSE"); 
//--> You are modifying the same map reference, hence now map is [1 = "CSE"]
map_1.put(21,map); 
//--> you are putting the same map ref to map1 with new key so map1 is [20 = [1 = "CSE"], [21 = [1 = "CSE"]]

Since you are using the same map object the output is like that. If you wish to create a new map , you may need to assign a new HashMap to map reference

You are printing only values hence the output

This is because you are storing the reference of the object, in the map, and not it's value. In your map_1, both key 20 and 21 links to the same object (map). You'll have to create a new map object for each key if you want them to differ(for instance in a loop).

And by the way, you are just printing the values, and not the keys, and that's why 20 and 21 are not shown in your output (assuming that was part of the question, of course)

Here's hoping it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM