简体   繁体   English

如何从Java中的哈希图获取arraylist?

[英]how to get the arraylist from the hashmap in java?

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]

No need for separate Lists for Keyword and Alternate . 不需要为KeywordAlternate单独列出。

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 ,它具有csv数据中的Keyword作为Keyword ,而List的值包含与Keyword对应的所有Alternate值。

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 , ie 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() . 该列表由entry.getValue()返回。 Just add this to the end of your main method: 只需将其添加到您的主要方法的末尾:

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. 注意:上面的代码尚未编译,但应提示您如何映射数据。

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");

ArrayList<String> values= new ArrayList<>(hashmap.values());

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM