When we use put() for a HashMap(), does it update update the entry or add a new entry ?
For example, if I have a Map like Map<String, String> m = new HashMap<String, String> (); m.put("k1","George"); m.put("k2", "John"); m.put("k1", "Allan");
I thought, in this case k1 will point to both "George" and "Allan" and we will be able to retrieve both "Geroge" and "Allan" using key "k1". But it doesn't looks like that is not the case, After the above code is executed, I could see "k1" pointing to "Allan" only. Can any one explain the reason for this ?
posted 9 years ago
The key in the map can only point to one value.
If you put it another entry where the key is already in the map, you will lose the first value.