如何从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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:07:43  来源:igfitidea点击:

how to get the arraylist from the hashmap in java?

javaarraylisthashmap

提问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 arraylistfor 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 Keywordand Alternate.

无需单独的列表KeywordAlternate

What you need is Mapwhich has as key the Keywordfrom your csv data and as value a Listwhich holds all the Alternatevalues 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 Alternateto 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());

Stringcould be any other object type

String可以是任何其他对象类型