Reputation: 3975
I have a List of objects with unique identifier (id), fetched from a DB. For a particular scenario I have to remove duplicates. For that purpose, I am storing it in a HashSet
. However, that process takes some time.
I wanted to know how this HashSet
identifies duplicates. toString
function has been overridden. Is there any other function or something that I should override to help speedup the HashSet
function?
Will hash function overriding be of any help?
Upvotes: 0
Views: 79
Reputation: 394136
You should be overriding the hashCode
and equals
methods. Those are the methods used to locate a key in the HashSet
/HashMap
.
The HashSet
identifies duplicates by first calculating the hashCode
of the object it is searching for, and then iterating over all the objects in the HashSet
that share this hashCode
and comparing them to the searched object with equals
, until a match is found.
Upvotes: 2