MapPutNoOverwrite
DRAFT

Misconception:

If one tries to put an entry into a map, and the map already contains an entry with the same key, then nothing happens.

Incorrect

Map.put with an existing key does nothing

Correct

Correction
Here is what's right.

Wrong. The existing entry will be replaced with the new entry.

Map<String,Integer> m = ...;
m.put("Hi", 2);
assert m.get("Hi")==2; // m now maps the key "Hi" to the value 2
m.put("Hi", 30);
assert m.get("Hi")==30; // m now maps the key "Hi" to the value 30

Language

Java

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.