In this article we will discuss how to search for an element in HashSet.

Main feature of a HashSet is Fast searching. Which it achieves using Hashing. Now suppose we want to search if an element exists in HashSet ?

It can be done using contains() member function of HashSet i.e.

It will first calculate the Hash Code of given object. Then based on that hash code, it will go to a bucket and check all elements inside the bucket using thier equals() member function.

Now Lets see an example to check if a given element exists in a HashSet i.e.

Ouput:

 

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,