Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.util.Map.put(key, value) - what if value equals existing value?

From the spec: "If the map previously contained a mapping for the key, the old value is replaced by the specified value." I'm wondering about the situation where value.equals(the old value) but value != the old value. My reading of the spec is that the old value must still be replaced. Is that right?

like image 633
philo Avatar asked Sep 25 '12 17:09

philo


1 Answers

If a new key matches an existing key, the mapped value will be replaced regardless of it's value, e.g. even if oldValue.equals(newValue) is true.

I don't think that we need to look at source, or rely on test code: this is explicit from the documentation for Map.put, where we find:

If the map previously contained a mapping for the 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.)

like image 112
pb2q Avatar answered Sep 21 '22 01:09

pb2q