Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to put duplicate keys in a hashmap

 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I 've just read this :

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

where the TreeMap row says we can .
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Joanne
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
tapeshwar sharma
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic