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??
-
Your question is pretty unclear. What are you trying to achieve? What have you tried so far?Jason Hall– Jason Hall2010-04-13 18:15:52 +00:00Commented Apr 13, 2010 at 18:15
-
Your question is a little vague. Do you want to use an ArrayList as a key in a Java HashMap? Maybe a few more sentences to clarify your question would be good.Rob Heiser– Rob Heiser2010-04-13 18:16:52 +00:00Commented Apr 13, 2010 at 18:16
Add a comment
|
2 Answers
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>