Why Searching is faster in HashSet than ArrayList?

If you try to find an object in an ArrayList then the ArrayList has to start searching from the beginning, looking at each element in the list to see if there is any match.
But HashSet can find an object more quickly because it uses the hashcode as a kind of label on the bucket where it stored the element. When you enter an object then the HashSet finds its hashcode and it knows where the same hashcode exists in the HashSet.
Remember if the HashCode finds more than one matching in the bucket then it calls the object’s equals().

No comments:

Post a Comment