如何从java中的hashmap获取arraylist?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18680542/
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 get the arraylist from the hashmap in java?
提问by user2602860
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;
//import java.util.Map.Entry;
public class teste{
public static void main(String[] args){
}
}
I just want the final arraylist
for each key for display purpose which is :
我只想要arraylist
每个键的最终显示目的是:
The returning values of the key :[silicone baby dolls for sale, ego ce4, venus, sample,blue]
键的返回值:[silicone baby dolls for sale, ego ce4, venus, sample,blue]
The returning values of the key :[apple, banana, key, kill]
键的返回值:[apple, banana, key, kill]
采纳答案by A4L
No need for separate Lists for Keyword
and Alternate
.
无需单独的列表Keyword
和Alternate
。
What you need is Map
which has as key the Keyword
from your csv data and as value a List
which holds all the Alternate
values corresponding to a Keyword
.
您需要的是Map
它具有Keyword
来自您的 csv 数据的键和作为值 a List
,它包含Alternate
与 a 对应的所有值Keyword
。
Map<String, List<String>> alternateMap = new HashMap<>();
Note that putting a value into a map with a key which is already present in that map will overwrite the previous value. So you have to put the list only the first time you find a new Keyword
, i.e. when trying to add an alternative for a keyword, first check whether the corresponding List exists in the map, if not then create a List and put into the map, then add the Alternate
to that list.
请注意,将一个值放入映射中的键已经存在于该映射中将覆盖先前的值。所以你只需要在你第一次找到一个新的时候把列表放进去Keyword
,即当试图为一个关键字添加一个替代时,首先检查地图中是否存在相应的列表,如果没有则创建一个列表并放入地图中,然后将其添加Alternate
到该列表中。
while(...) {
String keyword = ...;
String alternate = ...;
// check whether the list for keyword is present
List<String> alternateList = alternateMap.get(keyword);
if(alternateList == null) {
alternateList = new ArrayList<>();
alternateMap.put(keyword, alternateList);
}
alternateList.add(alternate);
}
// printing the result
for(Map.Entry<String, List<String>> alternateEntry : alternateMap.entrySet()) {
System.out.println(alternateEntry.getKey() + ": " +
alternateEntry.getValue().toString());
}
EDIT
编辑
After running your code it seems to working fine. The List is returned by entry.getValue()
. Just add this to the end of your main method:
运行您的代码后,它似乎工作正常。列表由 返回entry.getValue()
。只需将此添加到您的主要方法的末尾:
for(Entry<String, ArrayList<String>> entry : example.items.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue().toString());
}
and it should give you the output you want
它应该给你你想要的输出
ego kit: [silicone baby dolls for sale, ego ce4, venus, sample, blue]
samsung: [apple, banana, key, kill]
Note: the code above was not compiled but should give you a hint how to map your data.
注意:上面的代码没有编译,但应该给你一个提示如何映射你的数据。
回答by user2663609
HashMap hm=new HashMap();
ArrayList ln=new ArrayList();
ln.add("hi");
ln.add("heloo");
ln.add(123);
hm.put("somename", ln);
ArrayList ls=(ArrayList)hm.get("somename");
回答by Isaac Sekamatte
ArrayList<String> values= new ArrayList<>(hashmap.values());
ArrayList<String> values= new ArrayList<>(hashmap.values());
String
could be any other object type
String
可以是任何其他对象类型