Java:创建一个 HashMap 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4100486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: create a list of HashMaps
提问by john
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.
作为一个过程语言背景(SAS)的Java学习者,我花了相当多的时间学习和实验ArrayList、LinkedList、Map、LinkedMap等——我不明白它的工作原理。我不明白为什么以我有限的知识。现在,以下这些答案都很棒!他们用 Java 解释了非常重要的数据结构,至少对我来说是这样。
THANK YOU ALL!!!!
谢谢你们!!!!
采纳答案by Luke Hutteman
You need to create a new HashMap for every entry, instead of reusing the existing one. This would work:
您需要为每个条目创建一个新的 HashMap,而不是重用现有的。这会起作用:
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.
此外,您可以删除list.add(new HashMap());
将空地图添加到从未填充的列表中的as。
回答by Peter Knego
You are never saving a reference to this Map:
您永远不会保存对此 Map 的引用:
list.add(new HashMap());
回答by hvgotcodes
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.
将三个添加到列表中。第一个添加是到一个新的地图实例;你从不设置任何值。第二个 add 你传入一个对 nMap 的引用,它有 1,是的。第三个添加您传递相同的引用。所以 Map 现在有 3 个引用,第一个是你从未添加任何值的地图,接下来的 2 个是同一个地图。这就是为什么你得到相同的输出。
回答by Denis Kniazhev
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.
永远不会被引用。所以最终你会得到一个包含 3 个项目的列表,其中 2 个是相同的。
回答by Prine
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 Integersand Strings.
可能还值得一提的是,您应该定义在List 中使用的元素的类型,因为 HashMap 不可能,因为您正在混合Integers和Strings。
And another thing is that you should use the Listinterface as type, so you are able to change the implementation (ArrayListor whatever) in the future.
另一件事是您应该使用List接口作为类型,这样您就可以在将来更改实现(ArrayList或其他)。
Here the corrected code:
这里更正的代码:
Map mMap = new HashMap();
List<Map> list = new ArrayList();
回答by M.ArslanKhan
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.
它不会制作长度为 4 的地图,因为键(“开始”)是相同的,因此它将覆盖同一键上的值。所以你只会得到一个针对“开始”键的值(4)。为避免这种情况,您必须更改哈希图中的键名,但在您的场景中,您需要另一个哈希图实例来保存键和值,因为您正在维护 arralist。