spliterator
Spliterator is an interface in the Java programming language (in the java.util package) that represents a source of elements which can be traversed and optionally partitioned into multiple parts. It was introduced with the Streams API in Java 8 to enable efficient and scalable parallel processing of data sources such as collections, arrays, and I/O streams.
A spliterator provides both traversal and, potentially, partitioning capabilities. Core operations include:
- tryAdvance, which consumes the next element if present and applies a given action to it, returning
- forEachRemaining, which repeatedly applies an action to all remaining elements.
- trySplit, which attempts to partition the source into two parts and returns a new spliterator for
- estimateSize, which returns an estimate of the number of remaining elements.
- characteristics, which returns a bitwise combination of properties describing the source, such as ORDERED, DISTINCT, SORTED,
- getComparator (when the SORTED characteristic is present), which returns the element comparator or throws IllegalStateException if
Characteristics provide information that helps the framework optimize traversal and splitting. For example, SIZED indicates that
Specialized subinterfaces exist for primitive streams, namely Spliterator.OfInt, Spliterator.OfLong, and Spliterator.OfDouble, which use primitive consumers to
Spliterators form a bridge between data sources and streams, enabling source-aware parallelism and efficient iteration. They