Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Spliterator — traversing elements and partitioning work

java.util.Spliterator supports reading elements and partitioning the remaining range. Source characteristics describe properties such as ordering and size accuracy; splitting alone does not start parallel execution.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Spliterator<T> s = Arrays.spliterator(array);
boolean advanced = s.tryAdvance(action);
Spliterator<T> prefix = s.trySplit();
long estimate = s.estimateSize();
int flags = s.characteristics();
boolean sized = s.hasCharacteristics(Spliterator.SIZED);
s.forEachRemaining(action);

java.util.Spliterator<T> traverses elements and partitions work. Spliterator.tryAdvance(action) processes one element and returns true, or returns false when exhausted. Spliterator.forEachRemaining(action) processes the remainder in the current thread.

Spliterator.trySplit() partitions off a disjoint portion or returns null; it starts no thread. With Spliterator.ORDERED, it partitions off a prefix, and traversal preserves encounter order. Do not share one instance between concurrently operating threads.

Spliterator.estimateSize() estimates the element count. A return value of Long.MAX_VALUE can indicate a size that is unknown, infinite, or too expensive to compute, but it can also be an exact finite count. Spliterator.SIZED guarantees accuracy before traversal or splitting begins, without structural source changes. For example, in Java 21, LongStream.range(0, Long.MAX_VALUE).spliterator() reports SIZED and has an exact initial size of Long.MAX_VALUE. Spliterator.SUBSIZED additionally guarantees SIZED and SUBSIZED for every split descendant.

Spliterator.characteristics() returns a bit mask; Spliterator.hasCharacteristics(mask) checks all requested bits. Spliterator.NONNULL guarantees no null elements. Spliterator.IMMUTABLE rules out adding, replacing, and removing source elements, but does not make their fields immutable.

The example uses Arrays.spliterator, which reports ORDERED, SIZED, and SUBSIZED. It processes the partitioned prefix before the remainder, so the result does not depend on the split point. Do not modify the array during traversal.

Examples

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Spliterator;

public class Main {
    public static void main(String[] args) {
        Spliterator<String> rest = Arrays.spliterator(
                new String[]{"A", "B", "C", "D"});
        System.out.println(rest.estimateSize());
        System.out.println(rest.hasCharacteristics(
                Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED));

        Spliterator<String> prefix = rest.trySplit();
        List<String> visited = new ArrayList<>();
        if (prefix != null) {
            prefix.tryAdvance(visited::add);
            prefix.forEachRemaining(visited::add);
        }
        rest.forEachRemaining(visited::add);
        System.out.println(visited);
        System.out.println(rest.tryAdvance(visited::add));
    }
}

Result

4
true
[A, B, C, D]
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources