What is a HashSet?

HashSet is a built-in datatype in Java, and in layman terms you can say “HashSet is an unordered collection of unique elements.” Have a look at a basic example:HashSet contains() Method in Java - 2
Fig 1: An illustration showing valid and invalid examples of a HashSet
(Before you move on further, we expect you to know about the terms hashing and hash tables.) HashSet extends (extends means a class is inherited from another class) AbstractSet and implements the Set interface. Here are a few points you need to consider before working with HashSets:
  • It creates a collection (collection means a single unit of objects eg: ArrayList, LinkedList, Vector etc) that uses a hash table for storage.
  • Duplicate values are not allowed in a HashSet.
  • You can insert “null” values in a HashSet.
  • No insertion order is maintained. So if you’re looking to insert items in order, a HashSet will be a bad choice for your requirements.
  • If you want to get/retrieve an element in constant time (O(1)) then a HashSet is one of the best approaches you can take.

What is HashSet.contains() method?

HashSet.contains() is a boolean method to check if an item is present in an instance of HashSet or not. Simply put, whether the set contains the desired element. Java.util.HashSet provides an efficient implementation for it. Let us show you how you can effectively use it to resolve your queries.

contains() with Integers

import java.util.HashSet;

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

       // Declare your hash set
       HashSet digits = new HashSet();

       System.out.println("All elements in hashset:\t" + digits);

       // Try adding duplicates
       System.out.println("After adding duplicates: \t" + digits);

       System.out.println("\n-------Using Contains Method-------");
       // Check out if the following digits exist in the hashset
       System.out.println("digits.contains(0) : " + digits.contains(0));
       System.out.println("digits.contains(2) : " + digits.contains(2));
       System.out.println("digits.contains(3) : " + digits.contains(7));
       System.out.println("digits.contains(null) : " + digits.contains(null));
All elements in hashset: [0, null, 1, 2, 3, 4, 5] After adding duplicates: [0, null, 1, 2, 3, 4, 5] -------Using Contains Method------- digits.contains(0) : true digits.contains(2) : true digits.contains(3) : false digits.contains(null) : true

contains() with Strings

Have a look at another example of contains() method with Strings.
import java.util.HashSet;
public class HashSetDemo {
     public static void main(String[] args) {

	  // Try working with another hash set of String type
	  HashSet rainbow = new HashSet();

        // Adding elements into HashSet using add()

        // Let's traverse the hashset
        System.out.println("Traversing the rainbow:");
        for (String i : rainbow){
       // Check out if rainbow contains this color or not
    	 System.out.println("\n-------Using Contains Method-------");

        System.out.println("rainbow.contains(\"Yellow\"): \t" + rainbow.contains("Yellow"));
        System.out.println("rainbow.contains(\"White\"): \t"  + rainbow.contains("White"));
        System.out.println("rainbow.contains(\"Lavender\"): \t"  + rainbow.contains("Lavender"));
        System.out.println("rainbow.contains(\"Red\"): \t"  + rainbow.contains("Red"));

        // Remove a color from rainbow using remove()

        // Now consume  set contains() method again to check if it's still present
        System.out.println("rainbow.contains(\"Red\"): \t"  + rainbow.contains("Red"));

        System.out.println("\nAfter removing Red: " + rainbow);
Traversing the rainbow: Red Violet Yellow Blue Indigo Orange Green -------Using Contains Method------- rainbow.contains("Yellow"): true rainbow.contains("White"): false rainbow.contains("Lavender"): false rainbow.contains("Red"): true rainbow.contains("Red"): false After removing Red: [Violet, Yellow, Blue, Indigo, Orange, Green]
As you can see the order of output is different from the order of putting the elements into the Hashset.


The method contains() for HashSets is a pretty simple one to use. However, incase of any confusion you can always refer to this post. Happy Coding! :)HashSet contains() Method in Java - 3