MapIsMultiMap
DRAFT

Misconception:

You can call Map.put(key, value) several times with the same key but different values, and each such call will add an additional entry to the map.

Incorrect

Maps can store multiple values for a given key

Correct

Correction
Here is what's right.

Wrong. A map contains only one value for a given key. If you call:

Map<String,String> map = ...
map.put("Hi", "value1");
map.put("Hi", "value2");

then the map will contain just one entry for key “Hi”: the last entry, (“Hi”, “value2”), you added for that key. That is, if you call map.get("Hi"), you will get back the value “value2”.

Note that this means that the keys in a map are a set. Each key exists just once in that map. You can get the set of keys by calling map.keySet():

Set<String> keys = map.keySet();

Language

Java

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.