wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "Map with duplicate keys" Watch "Map with duplicate keys" New topic
Author

Map with duplicate keys

Ajay Kadur
Greenhorn

Joined: Jul 16, 2006
Posts: 2
Hi,
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,
Ajay
Ilja Preuss
author
Sheriff

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: 14116
    
  16

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
 
 
subject: Map with duplicate keys