views:

1178

answers:

6

I want to have Map with duplicate keys, I know there are many Map implementations(eclipse shows me about 50), so I bet there must be one that allows this. I know its easy to write your own Map that does this, but i would rather use some existing solution. Maybe something in commons-collections or google-collections?

+2  A: 

You could simply pass an array of values for the value in a regular HashMap, thus simulating duplicate keys, and it would be up to you to decide what data to use.

You may also just use a MultiMap, although I do not like the idea of duplicate keys myself.

AlbertoPL
A: 

Could you also explain the context for which you are trying to implement a map with duplicate keys? I am sure there could be a better solution. Maps are intended to keep unique keys for good reason. Though if you really wanted to do it; you can always extend the class write a simple custom map class which has a collision mitigation function and would enable you to keep multiple entries with same keys.

Note: You must implement collision mitigation function such that, colliding keys are converted to unique set "always". Something simple like, appending key with object hashcode or something?

Priyank
The context is that i thought that keys will be unique, but it turns out that the might not be. I dont want to refactor everything since it wont be used often.
01
i want to convert a small XML file into hashmap like datatype. Only the problem is structure of XML file is not fixed
articlestack
+7  A: 

You are searching for a Multimap, and indeed both commons-collections and google-collections have several implementations for that. Multimaps allow for multiple keys by maintaining a collection of values per key, i.e. you can put a single object into the map, but you retrieve a collection.

If you can use Java 5, I would prefer google-collections as they are generics aware:

com.google.common.collect.Multimap

nd
Also, this Multimap doesn't pretend to be a Map the way the apache one does.
Kevin Bourrillion
+1  A: 

If you want iterate about a list of key-value-pairs (as you wrote in the comment), then a List or an array should be better. First combine your keys and values:

public class Pair
{
   public Class1 key;
   public Class2 value;

   public Pair(Class1 key, Class2 value)
   {
      this.key = key;
      this.value = value;
   }

}

Replace Class1 and Class2 with the types you want to use for keys and values.

Now you can put them into an array or a list and iterate over them:

Pair[] pairs = new Pair[10];
...
for (Pair pair : pairs)
{
   ...
}
Mnementh
How would i implement add() or put(). I dont want to hardcore number of dimensions.
articlestack
In this case use a List. The second sample changes to List<Pair> pairs = new List<Pair>(); The for-loop stays the same. You can add a pair with this command: pairs.add(pair);
Mnementh
A: 

just to be complete, Apache Commons Collections also has a MultiMap. The downside of course is that Apache Commons does not use Generics.

newacct
Note that their MultiMap implements Map but breaks the contracts of the Map methods. That bugs me.
Kevin Bourrillion
A: 

If there are duplicate keys then a key may correspond to more than one value. The obvious solution is to map the key to a list of these values. For example in python:

map = dict()
map["driver"] = list()
map["driver"].append("john")
map["driver"].append("mike")
print map["driver"]          # It shows john and mike
print map["driver"][0]       # It shows john
print map["driver"][1]       # It shows mike

Thanasis

cyberthanasis