Jony
Jony

Reputation: 6774

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

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

Upvotes: 1

Views: 680

Answers (2)

OscarRyz
OscarRyz

Reputation: 199234

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]

Upvotes: 6

BalusC
BalusC

Reputation: 1108782

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>

Upvotes: 1

Related Questions