jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Map with duplicate keys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Map with duplicate keys" Watch "Map with duplicate keys" New topic

Map with duplicate keys

Ajay Kadur

Joined: Jul 16, 2006
Posts: 2
Java reference says that a Map, HashMap or Hashtable does not allow to store multiple entries with a same key. I have the requirement to store multiple entries (basically key-value pairs) into some collection with a same key.
Is there any collection to handle such a situation?

Thanks in advance,
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Jakarta Commons Collections has MultiMaps: http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_1/org/apache/commons/collections/MultiMap.html

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114

You could also just use a Map (HashMap) and for the values you use a List instead of single values.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
It is sorta covered in the JavaRanch Style Guide.
subject: Map with duplicate keys