I have a list of objects that I need to add to a HashSet, say List<Node> books
. Say further that no two books are equal in the very sense that their equals
method would each return false; say, however, that their hashCode
method each return 1
. I am using this extreme case so that I can understand fully how admission works. So my question is this: Will the HashSet admit all my objects?
public class Book{
...
@Override
public boolean equals(Book other){
return false;
}
@Override
public int hashCode(){
return 1;
}
}
recall that hashSet does not admit duplicates.
I am wondering about this because by the very name a hashSet uses a hash of the object. Is that hash by any chance related to the hashCode of the object being added?
And yes I am aware of
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 how are the hashCode of the object related to its hash value in the HashSet?