i have 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??
+5
A:
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]
OscarRyz
2010-04-13 18:20:00
you know you could just do `hashMap.put( "list", Arrays.asList("A","B","C"));`
matt b
2010-04-13 18:21:28
@matt b Yeah, but that wouldn't be an ArrayList ( as the OP needed )
OscarRyz
2010-04-13 18:23:19
+1
A:
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>
BalusC
2010-04-13 18:20:33