How to Search for an Element in HashSet in Java? | HashSet contains() Method Implementation in Java

The HashSet class implements the Set interface and is supported by a hash table, which is a HashMap instance. The null element is allowable in this class. The class also provides continuous time for basic operations like adding, removing, containing, and size provided that the Hash function correctly distributes the elements between the buckets.

In this tutorial we will explain all about how to Search for an Element in a HashSet using Contains() method. Refer to the Programs that Checks Whether a Specific Element is present in Hashet or not. Given a HashSet, the task is to search for an element in HashSet.

Example:

Input:

Given hashset elements : { 'hello' , 'this' , 'is', 'BTechGeeks' , 'is', 'platform' }   search_element ='BTechGeeks'

Output:

The given hashset contains BTechGeeks element in it.

How to Search for an Element in HashSet in Java?

A HashSet’s main feature is fast searching. This is accomplished through the use of Hashing. Assume we want to see if an element in HashSet exists. It is possible to accomplish this by using HashSet’s contains() member function.

HashSet contains() Method in Java

The Java.util.HashSet.contains() method is used to determine whether or not a specific element exists in the HashSet. So, in essence, it is used to determine whether a Set contains a specific element or not.

Syntax:

given_hashSet.contains(Object element)

Parameters:

HashSet is the type of the parameter element. This is the element that must be checked to see if it is present in the set.

Return:

If the element is present in the set, the method returns true otherwise, it returns False.

Also, See:

Example Program Implementing the Java.util.HashSet.contains() method

// Java code to illustrate HashSet.contains() method
import java.io.*;
import java.util.HashSet;

public class HashSetDemo {
public static void main(String args[])
{
// Creating an empty HashSet
HashSet<String> set = new HashSet<String>();

// Using add() method to add elements into the Set
set.add("Welcome");
set.add("To");
set.add("Geeks");
set.add("4");
set.add("Geeks");

// Displaying the HashSet
System.out.println("HashSet: " + set);

// Check for "Geeks" in the set
System.out.println("Does the Set contains 'Geeks'? " + set.contains("Geeks"));

// Check for "4" in the set
System.out.println("Does the Set contains '4'? " + set.contains("4"));

// Check if the Set contains "No"
System.out.println("Does the Set contains 'No'? " + set.contains("No"));
}
}

Output:

HashSet: [4, Geeks, Welcome, To]
Does the Set contains 'Geeks'? true
Does the Set contains '4'? true
Does the Set contains 'No'? false

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

Example to Check if an Item Exists in HashSet or Not

import java.io.*;
import java.lang.*;
import java.util.*;

class BtechGeeks {
    public static void main(String[] args)
        throws java.lang.Exception
    {
        // initailzing a hashset which is of type string
        HashSet<String> stringset = new HashSet<>();
        // adding elements to the hashset
        stringset.add("hello");
        stringset.add("this");
        stringset.add("is");
        stringset.add("BTechGeeks");
        stringset.add("is");
        stringset.add("platform");
        // given element
        String element = "BTechGeeks";
        // looking for the given element in the hash set

        if (stringset.contains(element)) {
            System.out.println("The given hashset contains "
                               + element
                               + " element in it.");
        }
        else {
            System.out.println(
                "The given hashset doesn't contain "
                + element + " element in it.");
        }
    }
}

Output:

The given hashset contains BTechGeeks element in it.

Explanation: Here BTechGeeks element is present in the given HashSet
Related Programs: