Java HashSet
In this page:
Introduction to HashSet
HashSet stores a collection of unique elements backed internally by a HashMap, silently discarding any element you try to add that's already present according to equals().
Example: Introduction to HashSet
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("apple"); // silently discarded
System.out.println(set.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Removing and Searching Elements
remove(element) deletes a matching element if present, and contains(element) checks membership — both run in average O(1) time thanks to the same hashing HashSet inherits from HashMap.
Example: Removing and Searching Elements
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
System.out.println(set.contains("apple"));
set.remove("apple");
System.out.println(set.contains("apple"));
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over HashSet
Iterating a HashSet visits every unique element exactly once, but like HashMap, it gives no guarantee about the order elements come back in — don't rely on insertion order or any particular sequence.
Example: Iterating over HashSet
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
for (String item : set) { // order not guaranteed
System.out.println(item);
}
}
}
Login to try C/C++/Java/PHP code in the editor
Set Operations
HashSet supports mathematical set operations through its collection methods: addAll() for union, retainAll() for intersection, and removeAll() for difference between two sets.
Example: Set Operations
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<Integer> a = new HashSet<>(java.util.List.of(1, 2, 3));
HashSet<Integer> b = new HashSet<>(java.util.List.of(2, 3, 4));
HashSet<Integer> intersection = new HashSet<>(a);
intersection.retainAll(b);
System.out.println(intersection);
}
}
Login to try C/C++/Java/PHP code in the editor
Converting HashSet to List
Converting a HashSet to a List (via new ArrayList<>(mySet)) is useful when you need indexed access or a defined order after using the set purely for its fast uniqueness guarantee.
Example: Converting HashSet to List
import java.util.HashSet;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
HashSet<String> set = new HashSet<>();
set.add("apple");
set.add("banana");
ArrayList<String> list = new ArrayList<>(set); // now has indexed access
System.out.println(list.get(0));
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting