1

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??

2
  • Your question is pretty unclear. What are you trying to achieve? What have you tried so far? Commented 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. Commented Apr 13, 2010 at 18:16

2 Answers 2

6
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]
Sign up to request clarification or add additional context in comments.

2 Comments

you know you could just do hashMap.put( "list", Arrays.asList("A","B","C"));
@matt b Yeah, but that wouldn't be an ArrayList ( as the OP needed )
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.