2

I would like to store multiple values for a single key and must be able to retrieve the specific value.

Is there any data structure in Java which have this kind of functionality? If so, please let me know.

serenity
  • 159
  • 1
  • 3
  • 16

3 Answers3

9

You may use Map<key,List<T>>

HashMap<String,ArrayList<String>> map;
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • What about http://stackoverflow.com/questions/4554455/hashtable-with-mutiple-values-for-single-key the hash table with list solution. Which one is the best..? – 3lokh Feb 13 '15 at 04:15
  • Of course `HashMap`. Have a look at [Differences between HashMap and Hashtable?](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable) – KV Prajapati Feb 13 '15 at 09:53
  • it converts itself into a binary tree after a specific threshold which takes search time as log(n) – technazi May 06 '19 at 18:22
8

There is no support in JDK. What you want is a Multimap and guava library provides it.

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
1

What is wrong with Map<Key, List<Element>>?

stryba
  • 1,979
  • 13
  • 19