17

I tried to create a list of maps. In the following code, I'm expecting to get

[{start=1,text=ye}, {start=2,text=no}]

however, I only got

[{start=2,text=no}, {start=2,text=no}]

How to avoid overriding the first map? Here is my code:

HashMap mMap = new HashMap();
ArrayList list = new ArrayList();
list.add(new HashMap());
mMap.put("start",1);
mMap.put("text","yes");
list.add(mMap); 
mMap.put("start",2);
mMap.put("text","no");
list.add(mMap); 
System.out.println("Final result: " + list );

thanks!

==========================

As a learner of Java who came from a procedure language background (SAS), I spent quite a few hours learning and experimenting ArrayList, LinkedList, Map, LinkedMap, etc--- I coldn't get it to work. And I don't understand why with my limited knowledge. Now, these following answers are all excellent! They explained very important data structure in Java, at least for me.

THANK YOU ALL!!!!

0

7 Answers 7

46

You need to create a new HashMap for every entry, instead of reusing the existing one. This would work:

HashMap mMap = new HashMap();
mMap.put("start",1);
mMap.put("text","yes");
list.add(mMap); 
mMap = new HashMap(); // create a new one!
mMap.put("start",2);
mMap.put("text","no");
list.add(mMap); 

also, you can remove the list.add(new HashMap()); as that adds an empty map to your list that is never populated.

Sign up to request clarification or add additional context in comments.

1 Comment

will it give a performance hit . consider if we have a lot many hashmaps to be created .
9

Something which is maybe also worth mention it, is that you should define the type of the elements you use in the List, for the HashMap its not possible because you are mixing Integers and Strings.

And another thing is that you should use the List interface as type, so you are able to change the implementation (ArrayList or whatever) in the future.

Here the corrected code:

Map mMap = new HashMap();
List<Map> list = new ArrayList();

Comments

3

Yes, hash map from this piece of code

list.add(new HashMap());

is never referenced. So eventually you get a list of 3 items, 2 of which are identical however.

Comments

0

You are never saving a reference to this Map:

list.add(new HashMap());

Comments

0

have three adds to the list. the first add is to a new map instance; you never set any values. The second add you pass in a reference to nMap, which has 1, yes. The third add you pass the same reference. So the Map now has 3 references, the first to a map you never added any values to, the next 2 to the same map. which is why you get the same output.

Comments

0

When you put the same key name in the map then values will be overridden to same key. suppose we have

mMap.put("start",1);
mMap.put("start",2);
mMap.put("start",3);
mMap.put("start",4);

it will not make map of length 4, as the key("start") is same so it will override the values on the same key. so you will get the get only one value (4) against "start" key. To avoid this you will have to change the name of keys in a hashmap but in your scenaro, you need an other instance of hashmap to save the key and values as you are maintaining the arralist.

Comments

0

You need to address a couple of things:

  • list.add(new HashMap());: Adds a new HashMap() to the list which is never populated.

  • These lines:

    mMap.put("start",2);
    mMap.put("text","no");
    

    Overrides the key, value pairs of the mMap object.

  • list.add(mMap); adds a duplicate entry of the first entry.

Hence you see two similar entries as:

[{start=2,text=no}, {start=2,text=no}]

Solution

To create a list of HashMaps you have to create the HashMaps seperately and add to the list as follows:

HashMap mMap1 = new HashMap();
mMap1.put("start", 1);
mMap1.put("text", "yes");
HashMap mMap2 = new HashMap();
mMap2.put("start", 2);
mMap2.put("text", "no");
ArrayList list = new ArrayList();
list.add(mMap1);
list.add(mMap2);
System.out.println(list); //output: [{start=1, text=yes}, {start=2, text=no}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.