Hash based collections like HashMap use hashCode method for faster searching and organization of elements. The equals method is only called if the hashCode matches. There are tons of tutorials on this like this, this and this...
HashSet doesn't allow duplicate elements, in order to check it,
1) It uses object's hashCode() method, if those values are different, then those object are belonging to different buckets. And Those are different objects.
2) If hashCode() method returns same values for two object, then they belongs to same bucket, it further go to check that they are different or not by invoking equals() method. If both return same boolean value, those two object are same. Only one of them can be in the HashSet.
It's up to us(programmers) to implements the hashCode() method and equlas() method to work appropriately.
Further, have a look on K&B's book for the hashCode() contract and equals() method contract.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards