当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Printout arraylist values in a Hashmap

问题描述:

I have a Hash map which contains String as key and and group of arraylist obects as values

 Map<String,List<ReportEPVO>> myListMap = new HashMap();

Here Suppose the key is 2013 and it contains 5 arraylist objects

How can i printout the values of those five objects from that hashmap.

网友答案:

You can use the keySet:

for (String s : myListMap.keySet()) {
    System.out.println("Current key: " + s); //Optional for better understanding
    for (ReportEPVO r : myListMap.get(s) {
        System.out.println(r.toString());
    }
}

The above code loops through all Lists stored in your Map and in each of them through all the elements. For debug purposes you can print current key each time.

If you only want to iterate through the list stored with key=2013 use:

for (ReportEPVO r : myListMap.get("2013") {
    System.out.println(r.toString());
}

Answering to the question from a comment. Add the following to your ReportEPVO.java:

@Override
public String toString() {
    return "FirstFieldName :" + <firstfield> + " SecondFieldName: " + <secondfield>.
}

In your field is String, simply put it's name. If it's a primitive (int, float, boolean...) put String.valueOf(fieldName);. Otherwise put fieldName.toString().

网友答案:

Using Java 8

String search = "300";
data.keySet().stream().filter(k -> k.equalsIgnoreCase(search)).map(k -> data.get(k)).forEach(v -> System.out.println(v));

Need to override toString() method in your ReportEPVO class.

分享给朋友:
您可能感兴趣的文章:
随机阅读: