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:
- 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();
digits.add(0);
digits.add(1);
digits.add(2);
digits.add(3);
digits.add(4);
digits.add(5);
digits.add(null);
System.out.println("All elements in hashset:\t" + digits);
// Try adding duplicates
digits.add(5);
digits.add(2);
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));
}
}
Output
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()
rainbow.add("Red");
rainbow.add("Orange");
rainbow.add("Yellow");
rainbow.add("Green");
rainbow.add("Blue");
rainbow.add("Indigo");
rainbow.add("Violet");
// Let's traverse the hashset
System.out.println("Traversing the rainbow:");
for (String i : rainbow){
System.out.println(i);
}
// 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()
rainbow.remove("Red");
// 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);
}
}
Output
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.
GO TO FULL VERSION