开发者

Java: create a list of HashMaps

开发者 https://www.devze.com 2023-01-23 19:19 出处:网络
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}]

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!!!!


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.


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();


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.


You are never saving a reference to this Map:

list.add(new HashMap());


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消