Java HashSet contains Method With Examples

In this tutorial we will learn about java HashSet contains method . HashSet.contains() method is used to check given element present or not in HashSet . Now , we will learn how to check specific element HashSet contains or not with examples .

Syntax :

public boolean contains(Object o) {

}

Parameters : The element whose presence in the HashSet has to be tested.
Returns Value :  It returns true if this set contains the specified element .

Example 1 : HashSet.contains() method – Element is present

In this example we will create HashSet of Strings and add elements into it . Now we will check element “vogue” is present or not into HashSet . If HashSet Contains element then contains() method will return true . Because there HashSet contains element , So HashSet.contains() returns true .

import java.util.HashSet;

public class HashSetcContains {
	public static void main(String[] args) {

		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add("java");
		hashSet.add("vogue");
		hashSet.add("welcomes");
		hashSet.add("you");
		hashSet.add("dear");
		
		System.out.println("HashSet Elements : "+ hashSet);
		
		String element = "vogue";
		
		boolean isContains = hashSet.contains(element);
		
		System.out.println("HashSet contains element "+ element +" ? "+ isContains);

	}
}

Output:

HashSet Elements : [Java, Vogue, Dear, You, Welcomes]
HashSet contains element Vogue ? true

Example 2 : HashSet.contains() method – Element is not present

In this example we will create HashSet of Strings and add elements into it . Now we will check element “android” is present or not into HashSet . If HashSet Contains element then contains() method will return true . Because there HashSet not contains element Android , So HashSet.contains() method returns false .

import java.util.HashSet;

public class HashSetcContains1 {
	
	public static void main(String[] args) {
		
		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add("java");
		hashSet.add("vogue");
		hashSet.add("welcomes");
		hashSet.add("you");
		hashSet.add("dear");
		
		System.out.println("HashSet Elements : "+ hashSet);
		
		String element = "android";
		
		boolean isContains = hashSet.contains(element);
		
		System.out.println("HashSet contains element "+ element +" ? "+ isContains);

	}

}

Output:

HashSet Elements : [welcomes, java, vogue, dear, you]
HashSet contains element android ? false

Example 3 : Java HashSet contains Method with null element

In this example we will create HashSet of Strings and add some elements into it . Now we will check element null  is present or not into HashSet . Because there HashSet not contains element null , So HashSet.contains() method returns false .

import java.util.HashSet;

public class HashSetcContains2 {
	
	public static void main(String[] args) {
		
		HashSet<String> hashSet = new HashSet<String>();
		hashSet.add("java");
		hashSet.add("vogue");
		hashSet.add("welcomes");
		hashSet.add("you");
		hashSet.add("dear");
		
		System.out.println("HashSet Elements : "+ hashSet);
		
		String element = null;
		
		boolean isContains = hashSet.contains(element);
		
	System.out.println("HashSet contains element "+ element +" ? "+ isContains);
		
	}
}

Output:

HashSet Elements : [welcomes, java, vogue, dear, you]
HashSet contains element null ? false

In this tutorial we have learned how to use java HashSet contains() method to check given element is present ot not into HashSet . You can see more java HashSet example for practice .

A Guide to HashSet                                                                                                            Java Doc – HashSet

Leave a Reply

Your email address will not be published. Required fields are marked *

7 + 3 =