如何从java中的hashmap获取arraylist?

use*_*860 0 java arraylist hashmap

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){

    }
}
Run Code Online (Sandbox Code Playgroud)

我只是希望arraylist每个键的最终用于显示目的,即:

键的返回值:[silicone baby dolls for sale, ego ce4, venus, sample,blue]

键的返回值:[apple, banana, key, kill]

A4L*_*A4L 5

不需要单独的列表KeywordAlternate.

你需要的是从你的csv数据中获得的Map关键,Keyword以及List包含Alternate与a相对应的所有值的值a Keyword.

Map<String, List<String>> alternateMap = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)

请注意,使用已存在于该映射中的键将值放入映射中将覆盖以前的值.因此,您必须在第一次找到新列表时才放置列表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());
}
Run Code Online (Sandbox Code Playgroud)

编辑

运行代码后,它似乎工作正常.列表由返回entry.getValue().只需将其添加到main方法的末尾:

for(Entry<String, ArrayList<String>> entry : example.items.entrySet()) {
    System.out.println(entry.getKey() + ": " +  entry.getValue().toString());
}
Run Code Online (Sandbox Code Playgroud)

它应该给你你想要的输出

ego kit: [silicone baby dolls for sale, ego ce4, venus, sample, blue]
samsung: [apple, banana, key, kill]
Run Code Online (Sandbox Code Playgroud)

注意:上面的代码没有编译,但应该提供一个如何映射数据的提示.