views:

139

answers:

2
+1  Q: 

hashmap in java

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
you know you could just do `hashMap.put( "list", Arrays.asList("A","B","C"));`
matt b
@matt b Yeah, but that wouldn't be an ArrayList ( as the OP needed )
OscarRyz
+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