Java HashSet.contains() – Examples

In this tutorial, we will learn about the Java HashSet.contains() method, and learn how to use this method to check if this HashSet contains specified object or element, with the help of examples.

contains(obj)

HashSet.contains() Returns true if this set contains the specified element.

Syntax

The syntax of contains() method is

</>
Copy
HashSet.contains(Object obj)

where

ParameterDescription
objThe element whose presence in the HashSet has to be found.

Returns

The function returns boolean value.

Example 1 – contains(obj)

In this example, we will define a HashSet of Strings and add some elements to it. The we shall check if element "c" is present in this HashSet using contains() method. Since, the element is present in the HashSet, contains() method should return true.

Java Program

</>
Copy
import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {
		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add("a");
		hashSet.add("b");
		hashSet.add("c");
		hashSet.add("d");
		
		String element = "c";
		boolean result = hashSet.contains(element);
		System.out.println("HashSet contains element \"" + element + "\" ?  " + result);
    }  
}

Output

HashSet contains element "c" ?  true

Example 2 – contains(obj) – Element not present

In this example, we will define a HashSet of Strings and add some elements to it. The we shall check if element "f" is present in this HashSet using contains() method. Since, the element is not present in the HashSet, contains() method should return false.

Java Program

</>
Copy
import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {
		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add("a");
		hashSet.add("b");
		hashSet.add("c");
		hashSet.add("d");
		
		String element = "f";
		boolean result = hashSet.contains(element);
		System.out.println("HashSet contains element \"" + element + "\" ?  " + result);
    }  
}

Output

HashSet contains element "f" ?  false

Example 3 – contains(obj) – HashSet with User-defined Objects

In this example, we will define and initialize a HashSet with objects of type Car. Then we will use contains() method to check if specific Car objects are present in this HashSet.

Java Program

</>
Copy
import java.util.HashSet;

public class Example {  
	public static void main(String[] args) {
		Car car1 = new Car(1, "Tesla");
		Car car2 = new Car(2, "BMW");
		Car car3 = new Car(3, "Toyota");
		
		HashSet<Car> hashSet = new HashSet<Car>();
		hashSet.add(car1);
		hashSet.add(car2);
		hashSet.add(car3);
		
		boolean result = hashSet.contains(car2);
		System.out.println("Is car2 present ?  " + result);
		
		result = hashSet.contains(new Car(2, "BMW"));
		System.out.println("Is the object present ?  " + result);
    }  
}

class Car {
	int id;
	String name;
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
}

Output

Is car2 present ?  true
Is the object present ?  false

In case of hashSet.contains(car2), we are passing the object instance for checking if it is present. As we have already added car2 to the HashSet, contains() method returns true.

In case of hashSet.contains(new Car(2, "BMW")), we are passing a new object instance for checking if it is present. Since, we have not added this new object instance to HashSet during initialization, contains() method returns false.

Conclusion

In this Java Tutorial, we have learnt the syntax of Java HashSet.contains() method, and also learnt how to use this method with the help of Java examples.