In Java 8 and later, spliterator() returns a Spliterator<E> over the elements of a HashSet. A spliterator is like an iterator that can also split its remaining elements into two parts, which is how parallel streams divide the work.
You rarely need it directly, because set.stream() and set.parallelStream() use it for you. It is useful for learning how streams are built, and for custom traversal code.
An instance method of java.util.HashSet (class since Java 1.2): a set of unique elements kept in a hash table, with no guaranteed order, one null element allowed, and no thread safety. spliterator() was added in Java 8, together with the Spliterator interface and streams.
Syntax
No arguments; the spliterator is then driven with its own methods.
Java 8 bulk removal that needs no manual traversal.
Java Examples
Example 1 — Step Through a Set with tryAdvance()
Handle one element per call until tryAdvance() returns false.
Java
import java.util.HashSet;
import java.util.Set;
import java.util.Spliterator;
public class HashSetSpliteratorDemo {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
numbers.add(5);
numbers.add(1);
numbers.add(3);
Spliterator<Integer> sp = numbers.spliterator();
while (sp.tryAdvance(n -> System.out.println("got " + n))) {
// each successful call handled one element
}
boolean more = sp.tryAdvance(n -> System.out.println("never printed"));
System.out.println("more left: " + more);
}
}
Output
got 1
got 3
got 5
more left: false
HashSet iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
One element per call.tryAdvance runs the lambda for the next element and returns true; the while loop repeats until it returns false.
Hash order again. The numbers were added as 5, 1, 3, yet they are delivered by bucket position as 1, 3, 5.
Exhausted. After the third element nothing is left, so the final tryAdvance returns false and never runs its lambda.
Empty loop body. All the work happens inside the condition, which is why the braces only hold a comment.
Example 2 — Read the Size and Flags, Then Sum the Rest
Check the estimate and three characteristics, then add up every price with forEachRemaining().
Exact size. Before any traversal the spliterator knows the set holds five elements, so estimateSize() is 5.
SIZED and DISTINCT. A HashSet knows its size and never holds duplicates, so both flags are true.
Not ORDERED. A hash table has no meaningful encounter order, so ORDERED is false.
Accumulating in a lambda. A lambda cannot change a local int, so the sum goes into the one-element array total; 10 + 20 + 30 + 40 + 50 gives 150.
Example 3 — Split the Elements in Two with trySplit()
Ask the spliterator for half of the work, collect both halves, and count the elements.
Java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Spliterator;
public class HashSetSpliteratorSplit {
public static void main(String[] args) {
Set<Integer> numbers = new HashSet<>();
for (int i = 1; i <= 10; i++) {
numbers.add(i);
}
Spliterator<Integer> second = numbers.spliterator();
Spliterator<Integer> first = second.trySplit();
List<Integer> a = new ArrayList<>();
List<Integer> b = new ArrayList<>();
if (first != null) {
first.forEachRemaining(a::add);
}
second.forEachRemaining(b::add);
System.out.println("first part: " + a);
System.out.println("second part: " + b);
System.out.println("total: " + (a.size() + b.size()));
}
}
Output
first part: [1, 2, 3, 4, 5, 6, 7]
second part: [8, 9, 10]
total: 10
HashSet iteration order is not guaranteed. This is the typical output on OpenJDK 8–21; other JDKs or versions may print the entries in a different order.
How It Works
Two spliterators.trySplit() detaches the first part of the work into a new spliterator, and the original keeps the rest.
Split by bucket range. The default table has 16 buckets, so one part covers buckets 0 to 7 and the other 8 to 15; this is why 1 to 7 go together and 8 to 10 follow.
Null check.trySplit() may return null when the data cannot be divided, so the code tests first before using it.
Nothing lost or repeated. The two lists add up to all ten numbers, which is the guarantee a parallel stream relies on.
Frequently Asked Questions
It returns a Spliterator over the elements. A spliterator can step through elements one at a time or split them into two parts, which parallel streams use to share work.
Usually not. set.stream() and set.parallelStream() call it for you. Use it directly only for custom traversal or to learn how streams work.
An iterator only moves forward one element at a time. A spliterator can also split off part of the remaining elements with trySplit() and report characteristics such as SIZED and DISTINCT.
When the remaining elements cannot be divided any further, or are too few, trySplit() returns null and you keep traversing with the original spliterator.
No. A HashSet reports SIZED and DISTINCT but not ORDERED, because its elements have no guaranteed order.