This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes how to put duplicate keys in a hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to put duplicate keys in a hashmap" Watch "how to put duplicate keys in a hashmap" New topic
Author

how to put duplicate keys in a hashmap

Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Hi, could someone please tell me how to enable a hashmap to store duplicate keys. I guess its done by overriding equals() and hashcode() methods ...but how ??


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You can't have duplicate keys in a HashMap. You can, however, create a "multimap" -- a map with Lists as values. Check out the MultiValueMap in the Apache Commons Collections library (Javadoc here, main page here).


[Jess in Action][AskingGoodQuestions]
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

I don't want a map with Lists as values because this will create a lot of ArrayList objects hence I'll get performance issues.
Is this possible with Treemap ?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Originally posted by Aniruddh Joshi:
I don't want a map with Lists as values because this will create a lot of ArrayList objects hence I'll get performance issues.
Is this possible with Treemap ?


Maps of any sort cannot have duplicate keys


Joanne
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

I 've just read this :

http://mindprod.com/jgloss/collection.html

where the TreeMap row says we can .
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Originally posted by Aniruddh Joshi:
I 've just read this :

http://mindprod.com/jgloss/collection.html

where the TreeMap row says we can .


What it actually says is
Does not allow duplicate keys, unless you cheat to fool it into thinking they are not really duplicates.
What I think that is referring to is that a SortedMap uses the compareTo method to decide if two keys are equal whereas a Map uses equals/hashcode, so if you change your key object class so that it's compareTo method is not compatible with equals/hashcode, you may be able to insert 'duplicate' keys. See the Javadoc for TreeMap for more details.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

thanks Joanne
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Joanne Neal:
so if you change your key object class so that it's compareTo method is not compatible with equals/hashcode, you may be able to insert 'duplicate' keys.


Which would be a really, *really* bad idea.


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
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I don't want a map with Lists as values because this will create a lot of ArrayList objects hence I'll get performance issues.

What reason do you have to believe that creating ArrayList objects will cause performance issues?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
tapeshwar sharma
Ranch Hand

Joined: Mar 10, 2006
Posts: 245
First of all, why would anyone need to put duplicates in a Map?
There is a chance that some thing is missing in the requirement/design.
r kundam
Greenhorn

Joined: Feb 27, 2010
Posts: 1
Even this is old post,reply might be useful for users.

If user wants to putAll() with already existing key but with different values it can be done like this:

Map<String, List<String>> msgDefGroupMap = new HashMap<String, List<String>>();//This is the main Map to which user wants to add values with already exising key value
Map<String, List<String>> msgDefForEachFile = (It is a HashMap with args String & List<String>)
Iterator<String> keyIterator = msgDefForEachFile.keySet().iterator();
List<String> mapValues = new ArrayList<String>();
while(keyIterator.hasNext()){
String keyName = keyIterator.next();
if(msgDefGroupMap.containsKey(keyName)){
mapValues = msgDefGroupMap.get(keyName);
mapValues.addAll(msgDefForEachFile.get(keyName));
System.out.println("Size of new List is:"+mapValues.size());
}else{
mapValues = msgDefForEachFile.get(keyName);
}
msgDefGroupMap.put(keyName, mapValues);
}
 
Consider Paul's rocket mass heater.
 
subject: how to put duplicate keys in a hashmap
 
Similar Threads
how hashmap identify the duplicate values? (i need internal logic)
Advice on comparing two text files
Read duplicate keys from properties file
HashMaps
Duplicate keys in Hashmap returns null get("key")