In a Hashmap the hash code of the key provided is used to place the value in the hashtable. In a Hashset the obects hashcode is used to place the value in the underlying has
The HashSet
is backed up by a HashMap
.
From the javadocs.
This class implements the Set interface, backed by a hash table (actually a HashMap instance).
So if you change the hashcode, I doubt whether you can access the object.
The add
implementation of HashSet
is
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
The key is the elem and value is just a dummy Object called PRESENT
and the contains
implementation is
public boolean contains(Object o) {
return map.containsKey(o);
}