JavaHashSet
Java HashSet
AHashSet is a collection of elements where every element isunique.
It is part of thejava.util package and implements theSet interface.
Create a HashSet
Example
Create aHashSet object calledcars that will store strings:
import java.util.HashSet; // Import the HashSet classHashSet<String> cars = new HashSet<String>();Now you can use methods likeadd(),contains(), andremove() to manage your collection of unique elements.
Add Elements
To add elements to aHashSet, use theadd() method:
Example
// Import the HashSet classimport java.util.HashSet;public class Main { public static void main(String[] args) { HashSet<String> cars = new HashSet<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("BMW"); // Duplicate cars.add("Mazda"); System.out.println(cars); }}Note: In the example above, even though"BMW" is added twice, it only appears once in the set because sets do not allow duplicate elements.
Check If an Element Exists
To check whether an element exists in aHashSet, use thecontains() method:
Remove an Element
To remove an element, use theremove() method:
To remove all elements, use theclear() method:
HashSet Size
Usesize() to count how many unique elements are in the set:
Note: Duplicate values are not counted - only unique elements are included in the size.
Loop Through a HashSet
Loop through the elements of anHashSet with afor-each loop:
Other Types
Elements in anHashSet are actually objects. In the examples above, we created elements (objects) of type "String". Remember that aString in Java is an object (not a primitive type). To use other types, such asint, you must specify an equivalentwrapper class:Integer. For other primitive types, use:Boolean for boolean,Character for char,Double for double, etc:
Example
Use aHashSet that storesInteger objects:
import java.util.HashSet;public class Main { public static void main(String[] args) { // Create a HashSet object called numbers HashSet<Integer> numbers = new HashSet<Integer>(); // Add values to the set numbers.add(4); numbers.add(7); numbers.add(8); // Show which numbers between 1 and 10 are in the set for (int i = 1; i <= 10; i++) { if (numbers.contains(i)) { System.out.println(i + " was found in the set."); } else { System.out.println(i + " was not found in the set."); } } }}The var Keyword
From Java 10, you can use thevar keyword to declare aHashSet variable without writing the type twice. The compiler figures out the type from the value you assign.
This makes code shorter,but many developers still use the full type for clarity. Sincevar is valid Java, you may see it in other code, so it's good to know that it exists:
Example
// Without varHashSet<String> cars = new HashSet<String>();// With varvar cars = new HashSet<String>();The Set Interface
Note: Sometimes you will see bothSet andHashSet in Java code, like this:
import java.util.Set;import java.util.HashSet;Set<String> cars = new HashSet<>();This means the variable (cars) is declared as aSet (the interface), but it stores aHashSet object (the actual set). SinceHashSet implements theSet interface, this is possible.
It works the same way, but some developers prefer this style because it gives them more flexibility to change the type later.
When Order Matters
In the next chapter, you will learn aboutTreeSet, which stores unique elementsin sorted order.

