开发者

Given a key, how to display the ArrayList stored as the value?

i h开发者_JS百科ave a hashmap where every key has many values(stored in a arraylist). How to display the arraylist i.e the values for a particular key in a hashmap in java??


import java.util.*;

public class PrintListFromHashMap {

    public static void main( String [] args ) {

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

        hashMap.put( "list",       new ArrayList<String>(Arrays.asList("A","B","C")));

        System.out.println( hashMap.get("list") );

    }

}

$ javac PrintListFromHashMap.java 
$ java PrintListFromHashMap
[A, B, C]


So, you want to be able to associate multiple values with one key? If so, then just use either a Map<K, Collection<V>>, or Google Collections MultiMap<K, V>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜