Wednesday, July 06, 2005

A thought about Equals and hashCode

The equals () and hashCode() relationship has great importance while storing objects in the hashtable/hashMap/ hashSet , for one if you dont implement hashCode at all you might end up having a duplicate values in a Set or even having objects getting overwritten in case of Hashtable or hashMap.

It is necessary to exercise caution while writing this method. We learnt this when one of the object (we are storing in a hashMap) had the same value of int for all objects in the hashCode(). This eventually was a bottle neck as we realized since we no longer got the performance we desired from Map since all the elements were in the same bucket it made retrieval expensive..