I was going through the add method of HashSet. It is mentioned that
add
HashSet
If this set already contains the element, the call leaves t
See HashMap#put:
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced.
It replaces the key with the new value, this way, no duplicates will be in the HashSet.