Java 从哈希图中存储和检索 ArrayList 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19541582/
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
Storing and Retrieving ArrayList values from hashmap
提问by Insane Coder
I have a hashmap of the following type
我有以下类型的哈希图
HashMap<String,ArrayList<Integer>> map=new HashMap<String,ArrayList<Integer>>();
The values stored are like this :
存储的值是这样的:
mango | 0,4,8,9,12
apple | 2,3
grapes| 1,7
peach | 5,6,11
I want to store as well as fetch those Integers using Iterator or any other way with minimum lines of code.How can I do it?
我想使用迭代器或任何其他方式以最少的代码行存储和获取这些整数。我该怎么做?
EDIT 1
编辑 1
The numbers are added at random (not together) as key is matched to the appropriate line.
当键匹配到适当的行时,这些数字是随机添加的(不是一起添加的)。
EDIT 2
编辑 2
How can I point to the arraylist while adding ?
添加时如何指向数组列表?
I am getting error while adding a new number 18
in the line map.put(string,number);
18
在行中添加新号码时出现错误map.put(string,number);
回答by Masudul
for (Map.Entry<String, ArrayList<Integer>> entry : map.entrySet()) {
System.out.println( entry.getKey());
System.out.println( entry.getValue());//Returns the list of values
}
回答by Simon Forsberg
Our variable:
我们的变量:
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
To store:
储藏:
map.put("mango", new ArrayList<Integer>(Arrays.asList(0, 4, 8, 9, 12)));
To add numbers one and one, you can do something like this:
要添加数字一和一,您可以执行以下操作:
String key = "mango";
int number = 42;
if (map.get(key) == null) {
map.put(key, new ArrayList<Integer>());
}
map.get(key).add(number);
In Java 8 you can use putIfAbsent
to add the list if it did not exist already:
在 Java 8putIfAbsent
中,如果列表不存在,您可以使用添加列表:
map.putIfAbsent(key, new ArrayList<Integer>());
map.get(key).add(number);
Use the map.entrySet()
method to iterate on:
使用该map.entrySet()
方法迭代:
for (Entry<String, List<Integer>> ee : map.entrySet()) {
String key = ee.getKey();
List<Integer> values = ee.getValue();
// TODO: Do something.
}
回答by Shashank Degloorkar
Fetch all at once =
一次获取所有=
List<Integer> list = null;
if(map!= null)
{
list = new ArrayList<Integer>(map.values());
}
For Storing =
用于存储 =
if(map!= null)
{
list = map.get(keyString);
if(list == null)
{
list = new ArrayList<Integer>();
}
list.add(value);
map.put(keyString,list);
}
回答by Kakalokia
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
if(pairs.getKey().equals("mango"))
{
map.put(pairs.getKey(), pairs.getValue().add(18));
}
else if(!map.containsKey("mango"))
{
List<Integer> ints = new ArrayList<Integer>();
ints.add(18);
map.put("mango",ints);
}
it.remove(); // avoids a ConcurrentModificationException
}
EDIT: So inside the while try this:
编辑:所以在里面试试这个:
map.put(pairs.getKey(), pairs.getValue().add(number))
You are getting the error because you are trying to put an integer to the values, whereas it is expected an ArrayList
.
您收到错误是因为您试图将整数放入值中,而预期为ArrayList
.
EDIT 2: Then put the following inside your while loop:
编辑 2:然后将以下内容放入您的 while 循环中:
if(pairs.getKey().equals("mango"))
{
map.put(pairs.getKey(), pairs.getValue().add(18));
}
else if(!map.containsKey("mango"))
{
List<Integer> ints = new ArrayList<Integer>();
ints.add(18);
map.put("mango",ints);
}
EDIT 3:
By reading your requirements, I come to think you may not need a loop. You may want to only check if the map contains the key mango
, and if it does add 18
, else create a new entry in the map with key mango
and value 18
.
编辑 3:通过阅读您的要求,我开始认为您可能不需要循环。您可能只想检查映射是否包含键mango
,如果确实添加18
,则在映射中创建一个带有键mango
和值的新条目18
。
So all you may need is the following, without the loop:
所以你可能需要的是以下内容,没有循环:
if(map.containsKey("mango"))
{
map.put("mango", map.get("mango).add(18));
}
else
{
List<Integer> ints = new ArrayList<Integer>();
ints.add(18);
map.put("mango", ints);
}
回答by Prateek
You can use like this(Though the random number generator logic is not upto the mark)
你可以这样使用(虽然随机数生成器逻辑不符合标准)
public class WorkSheet {
HashMap<String,ArrayList<Integer>> map = new HashMap<String,ArrayList<Integer>>();
public static void main(String args[]) {
WorkSheet test = new WorkSheet();
test.inputData("mango", 5);
test.inputData("apple", 2);
test.inputData("grapes", 2);
test.inputData("peach", 3);
test.displayData();
}
public void displayData(){
for (Entry<String, ArrayList<Integer>> entry : map.entrySet()) {
System.out.print(entry.getKey()+" | ");
for(int fruitNo : entry.getValue()){
System.out.print(fruitNo+" ");
}
System.out.println();
}
}
public void inputData(String name ,int number) {
Random rndData = new Random();
ArrayList<Integer> fruit = new ArrayList<Integer>();
for(int i=0 ; i<number ; i++){
fruit.add(rndData.nextInt(10));
}
map.put(name, fruit);
}
}
OUTPUT
输出
grapes | 7 5
apple | 9 5
peach | 5 5 8
mango | 4 7 1 5 5
回答by juanpaolo
You could try using MultiMap instead of HashMap
您可以尝试使用 MultiMap 而不是 HashMap
Initialising it will require fewer lines of codes. Adding and retrieving the values will also make it shorter.
初始化它需要更少的代码行。添加和检索值也会使其更短。
Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();
would become:
会成为:
Multimap<String, Integer> multiMap = ArrayListMultimap.create();
You can check this link: http://java.dzone.com/articles/hashmap-%E2%80%93-single-key-and
您可以查看此链接:http: //java.dzone.com/articles/hashmap-%E2%80%93-single-key-and