How to print all the values for a key in HashMap in java

In a Map the key should always be unique. If you associate a new value to an existing key, it will overwrite the value of the existing entry.

You might need to check the interface for Map#put(K, V) method.

If the map previously contained a mapping for the key, the old value
is replaced by the specified value.

So in your case your map will always have "cde" as the value for the key "1".

Browse More Popular Posts

Leave a Comment