Java HashSet Methods

About This Index
This page is the Java HashSet Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, and all of them compile on Java 8 and later.
New to sets? A HashSet (in java.util, since Java 1.2) is a collection of unique elements: adding something that is already there changes nothing. It keeps its elements in a hash table, so add(), contains(), and remove() take constant time on average. It allows one null element, keeps no guaranteed order, and is not thread-safe.
A HashSet prints and iterates its elements in the order of its internal buckets, not in insertion or alphabetical order, and that order can change as the set grows. Examples that print a whole set show the typical output on OpenJDK 8–21 and say so; many examples print through a sorted TreeSet copy or a sorted list instead.
How to Call a HashSet Method
Every method here is an instance method, so you call it on your set variable:
import java.util.HashSet;
import java.util.Set;
Set<String> tags = new HashSet<>();
boolean first = tags.add("java"); // true: it was added
boolean again = tags.add("java"); // false: already there
boolean has = tags.contains("java"); // true
int count = tags.size(); // 1
boolean gone = tags.remove("java"); // true: it was removed Java HashSet Methods Table
All method guides in this Java tutorial series (17 tutorials), grouped by purpose.
Add and look up
4 methodsPut elements into the set, add a whole collection at once, and test whether one or many elements are already there. Lookups are fast.
| Method | Description | Tutorial |
|---|---|---|
add() | Add one element; returns false for a duplicate. | Try it |
addAll() | Add every element of another collection. | Try it |
contains() | Check whether an element is in the set. | Try it |
containsAll() | Check that every element of another collection is present. | Try it |
Remove and filter
5 methodsDelete one element, delete every element found in another collection, delete by a condition, keep only a shared part, or empty the set.
| Method | Description | Tutorial |
|---|---|---|
remove() | Delete one element; returns whether it was there. | Try it |
removeAll() | Remove every element found in another collection. | Try it |
removeIf() | Remove every element that passes a test (Java 8). | Try it |
retainAll() | Keep only the elements found in another collection. | Try it |
clear() | Remove every element from the set. | Try it |
Loop and convert
3 methodsWalk through the elements one by one, hand them to a Java 8 spliterator, or copy them into an array. All of them follow the same unguaranteed hash order.
| Method | Description | Tutorial |
|---|---|---|
iterator() | Walk through the elements one at a time. | Try it |
spliterator() | Get a Java 8 Spliterator that can step or split. | Try it |
toArray() | Copy the elements into an array. | Try it |
Copy and compare
3 methodsMake a shallow copy of the set, compare two sets for equal contents, or get the hash code of the whole set.
Size
2 methodsCount the elements or check whether there are none.
Frequently Asked Questions
Start with add()
Add your first elements and see what add() returns for a duplicate.
12 people found this page helpful
