The docs are pretty clear on this: HashSet.add
doesn't replace:
Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.
But HashMap.put
will replace:
If the map previously contained a mapping for the key, the old value is replaced.