Java Reference ยท Collections
The HashSet class
Reference for java.util.HashSet: a hash-based set with no duplicates, constant-time lookups, no guaranteed order, and the equals/hashCode contract.
HashSet<E> is the general-purpose Set implementation. It stores each element at most once, allows a single null, and answers add, remove and contains in expected constant time. Internally it is a HashMap whose keys are your elements, so the same rules for hashCode() and equals() apply.
A HashSet makes no promise about iteration order, and the order can change when the set grows. If you need insertion order use LinkedHashSet; if you need sorted order use TreeSet.
Set<String> tags = new HashSet<>(); Set<Integer> ids = new HashSet<>(List.of(3, 1, 3)); // {1, 3}
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, java.io.Serializable
| Constructor | Description |
|---|---|
HashSet() |
Empty set with default capacity 16 and load factor 0.75. |
HashSet(int initialCapacity) |
Empty set sized for at least that many buckets. Throws IllegalArgumentException if negative. |
HashSet(int initialCapacity, float loadFactor) |
Also sets the load factor. |
HashSet(Collection<? extends E> c) |
Set containing the elements of c (duplicates removed). Throws NullPointerException if c is null. |
| Member | Description |
|---|---|
boolean add(E e) |
Adds e; returns true if the set changed. |
boolean remove(Object o) |
Removes o; returns true if it was present. |
boolean contains(Object o) |
Membership test. |
int size(), boolean isEmpty(), void clear() |
Size and clearing. |
addAll, retainAll, removeAll, containsAll |
Bulk operations (see Set operations). |
static <T> HashSet<T> newHashSet(int numElements) |
Creates a set sized so that numElements elements fit without resizing (since Java 19). |
Usage notes
- Complexity:
add,removeandcontainsare expectedO(1)with a decent hash function. Iteration isO(capacity + size), so a huge, mostly empty set iterates slowly. - Equality contract: equal objects must have equal hash codes. If you override
equals()withouthashCode(), duplicates can sneak in. - Do not mutate elements in a way that changes their
equals/hashCodewhile they are in the set; they become impossible to find or remove. - No order. Never depend on the printed order of a
HashSet; sort or copy to aTreeSetwhen you need stable output. - Null: one
nullelement is allowed. - Not synchronized. For concurrent use wrap it with
Collections.synchronizedSet()or build a set fromConcurrentHashMap.newKeySet(). - Fail-fast iterators: structural changes during iteration throw
ConcurrentModificationException(best effort). - Sizing:
new HashSet<>(n)sets the table size, not the number of elements it holds before resizing;HashSet.newHashSet(n)(Java 19 and later) accounts for the load factor.
Related
- Set, LinkedHashSet, TreeSet, Set operations, HashMap
- Lessons: HashSet, How HashMap works