Take the 2-minute tour ×

I was wondering what happens to the earlier values in the case of duplicate/overwritten keys. Didn't find any documentation regarding the same.

Case 1: We overwrite values for a key.

Case 2: Duplicate key.

Map mymap = new HashMap();
mymap.put("1","not one");
mymap.put("1","surely not one");
//the following line is case 2 for duplicate

Case 1: We get "surely not one".

Case 2: We get "one".

But what happens to the other values? I was teaching basics to a student and I was asked this. Is the map like a bucket where the last value is dereferenced (but in memory?)


share|improve this question
BTW, this is an excellent opportunity to show off the multi-hashmap that is part of the Jakarta collections classes (commons.apache.org/collections). It will let you have any number of values associated with the same key for those times when you need that. –  John Munsch Nov 3 '09 at 20:41
possible duplicate of HashMap with multiple values under the same key –  Muhammed Refaat Oct 3 '13 at 20:52

4 Answers 4

By definition, the put command replaces the previous value associated with the given key in the map (conceptually like an array indexing operation for primitive types).

The map simply drops its reference to the value. If nothing else holds a reference to the object, that object becomes eligible for garbage collection. Additionally, Java returns any previous value associated with the given key (or null if none present), so you can determine what was there and maintain a reference if necessary.

More information here: HashMap Doc

share|improve this answer

You may find your answer in the javadoc of Map#put(K, V) (which actually returns something):

public V put(K key,
             V value)

Associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for this key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

key - key with which the specified value is to be associated.
value - value to be associated with the specified key.

previous value associated with specified key, or null if there was no mapping for key. (A null return can also indicate that the map previously associated null with the specified key, if the implementation supports null values.)

So if you don't assign the returned value when calling mymap.put("1", "a string"), it just becomes unreferenced and thus eligible for garbage collection.

share|improve this answer
The returned value is the previous value (or null) as documented just above in the javadoc so, yes, this is what I mean. Can it really be misinterpreted? –  Pascal Thivent Nov 3 '09 at 21:01

The prior value for the key is dropped and replaced with the new one.

If you'd like to keep all the values a key is given, you might consider implementing something like this:

import org.apache.commons.collections.MultiHashMap;
import java.util.Set;
import java.util.Map;
import java.util.Iterator;
import java.util.List;
public class MultiMapExample {

   public static void main(String[] args) {
      MultiHashMap mp=new MultiHashMap();
      mp.put("a", 10);
      mp.put("a", 11);
      mp.put("a", 12);
      mp.put("b", 13);
      mp.put("c", 14);
      mp.put("e", 15);
      List list = null;

      Set set = mp.entrySet();
      Iterator i = set.iterator();
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry)i.next();

          System.out.println(me.getKey()+": value :"+list.get(j));
share|improve this answer
Thanks but this is part of apache common library... –  Jay Thakkar Apr 15 at 11:23
and other Way , to use HashMap<String , List<String>> ... –  kamlesh0606 Apr 15 at 11:25

To your question whether the map was like a bucket: no.

It's like a list with name=value pairs whereas name doesn't need to be a String (it can, though).

To get an element, you pass your key to the get()-method which gives you the assigned object in return.

And a Hashmap means that if you're trying to retrieve your object using the get-method, it won't compare the real object to the one you provided, because it would need to iterate through its list and compare() the key you provided with the current element.

This would be inefficient. Instead, no matter what your object consists of, it calculates a so called hashcode from both objects and compares those. It's easier to compare two ints instead of two entire (possibly deeply complex) objects. You can imagine the hashcode like a summary having a predefined length (int), therefore it's not unique and has collisions. You find the rules for the hashcode in the documentation to which I've inserted the link.

If you want to know more about this, you might wanna take a look at articles on javapractices.com and technofundo.com


share|improve this answer

Your Answer


