java 如何从列表中将多个值放入 Map
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17464365/
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
How to put multiple values in Map from a list
提问by user2416728
I have a list gotitems
.
我有一个清单gotitems
。
ArrayList<String> gotitems = new ArrayList<String>();
i need to put that list in a hashmap called map
.
我需要将该列表放在名为map
.
Map<String,String> map = new HashMap<String,String>();
i had tried this :
我试过这个:
for(String s:gotitems){
map.put("a",s);
}
gotitems contains :
gotitems 包含:
First
Second
Third
But the output of :
但是输出:
System.out.println(map.values());
gives :
给出:
Third
Third
Third
i had even tried this :
我什至试过这个:
for(String s:gotitems){
for(int j=0;j<gotitems.size();j++){
map.put("a"+j,s);
}
}
but this is also not working.
但这也不起作用。
What am i doing wrong here ?
我在这里做错了什么?
回答by Suresh Atta
As per Map put(K,V) method docs
Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value.
将指定值与此映射中的指定键相关联(可选操作)。如果映射先前包含键的映射,则旧值将替换为指定值。
You are ovverriding the key each time here .
每次在这里您都忽略了密钥。
for(String s:gotitems){
map.put("a",s);
}
change the key each time and try like
每次更改密钥并尝试像
for(String s:gotitems){
map.put(s,s);
}
回答by htz
You are trying to put three Strings in the map under the same key "a"
. Try to use unique keys for your values.
您正试图将三个字符串放在地图中的同一个键下"a"
。尝试为您的值使用唯一键。
回答by Mena
You're putting all your items in the Map
with the same key: "a".
You should have a unique String
key for each value.
您将所有项目放入Map
同一个键中:“a”。String
每个值都应该有一个唯一的键。
For instance:
例如:
List<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three");
Map<String, String> map = new LinkedHashMap<String, String>();
for (String s: list) {
map.put(s, s);
}
System.out.println(map);
Output:
输出:
{one=one, two=two, three=three}
Note the LinkedHashMap
here: it maintains the order in which you put your key/value pairs.
请注意LinkedHashMap
这里:它维护您放置键/值对的顺序。
EditOf course if your List
does not have unique values, moving its values as keys to a Map
will overwrite some of the Map
's values. In that case you want to ensure your List
has unique keys first, or maybe use a Map<Integer, String>
with the index of the List
's value as key to the Map
, and the actual List
value as value to the Map
.
编辑当然,如果您List
没有唯一值,将其值作为键移动到 aMap
将覆盖 的某些Map
值。在这种情况下,您希望首先确保您List
拥有唯一的键,或者可能使用 a的值Map<Integer, String>
的索引List
作为 的键Map
,并将实际List
值用作 的值Map
。
回答by Bathsheba
When you write
当你写
for(String s:gotitems){
map.put("a",s);
}
you will trash any existing entry in the map held against the key "a". So after your iteration, your map will contain just one entry corresponding to the last iterated value in gotitems
.
您将删除地图中针对键“a”的任何现有条目。因此,在您的迭代之后,您的地图将仅包含一个对应于 中最后一个迭代值的条目gotitems
。
To use a map effectively you need to consider what your keys will be. Then use map.put(myKeyForThisItem, s)
instead. If you don't have an effective scheme for the keys then using a map is pointless as one tends to use the keys to extract the corresponding values.
要有效地使用地图,您需要考虑您的密钥是什么。然后map.put(myKeyForThisItem, s)
改用。如果您没有有效的键方案,那么使用映射是没有意义的,因为人们倾向于使用键来提取相应的值。
As for your second approach, it would be helpful if you could define "it is not working" a little clearer: perhaps iterate through the map and print the keys and values.
至于你的第二种方法,如果你能更清楚地定义“它不起作用”会很有帮助:也许遍历地图并打印键和值。
回答by Multithreader
Please note that in a map, a key can point to at most one value. In your case, you are doing the following mappings:
"a" -> "one"
then you overwrite it as
"a" -> "two"
then you overwrite it as
"a" -> "three"
请注意,在地图中,一个键最多可以指向一个值。在您的情况下,您正在执行以下映射:
“a”->“one”然后将其覆盖为
“a”->“two”然后将其覆盖为
“a”->“three”
remember: a key can point to at most one value. However, a value can be pointed at by multiple keys.
请记住:一个键最多可以指向一个值。但是,一个值可以由多个键指向。
回答by harsh
This is wrong:
这是错误的:
for(String s:gotitems){
map.put("a",s);
}
Since you are using "a"
common key for all values, last inserted key-value pair would be preserved, all previous ones would be overridden.
由于您"a"
对所有值都使用公共键,因此将保留最后插入的键值对,而将覆盖所有先前的键值对。
This is also not correct:
这也是不正确的:
for(String s:gotitems){
for(int j=0;j<gotitems.size();j++){
map.put("a"+j,s);
}
}
you are putting n*n times into map, though you want only n (gotitems.size()
) items into map.
您将 n*n 次放入地图中,尽管您只想将 n ( gotitems.size()
) 个项目放入地图中。
First decide on key which you want to use in map, copying List
into Map
one approach could be use index as key
:
首先决定要在地图中使用的键,复制List
到Map
一种方法可以使用 index as key
:
for(int j=0;j<gotitems.size();j++){
map.put("KEY-"+j,gotitems.get(j));
}
Output should be:
输出应该是:
KEY-0 First
KEY-1 Second
KEY-2 Third
KEY-0 首先
KEY-1 秒
KEY-2 第三
回答by Jon
I have reproduce your codes. The problem is that you are assigning the same key to different value. This should work.
我已经复制了你的代码。问题是您将相同的键分配给不同的值。这应该有效。
import java.util.*;
public class testCollection{
public static void main(String[] args){
ArrayList<String> gotitems = new ArrayList<String>();
gotitems.add("First");
gotitems.add("Second");
gotitems.add("Third");
Map<String,String> map = new HashMap<String,String>();
String x = "a";
int i = 1;
for(String s:gotitems){
map.put(x+i,s);
i++;
}
System.out.println(map);
}
}
回答by Juned Ahsan
This is because you are putting all the items in the map against the same key "a"
这是因为您将地图中的所有项目都放在同一个键“a”上
map.put("a");
You need to store each element against a unique key so add something like this:
您需要根据唯一键存储每个元素,因此添加如下内容:
int count = 0;
for(String s:gotitems){
map.put("a" + count,s);
count++;
}