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).
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.
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
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>();
String keyName = keyIterator.next();
mapValues = msgDefGroupMap.get(keyName);
System.out.println("Size of new List is:"+mapValues.size());
mapValues = msgDefForEachFile.get(keyName);