Like Iterator and ListIterator, Spliterator is a Java Iterator, which is used to iterate elements one-by-one from a List implemented object.
Some important points about Java Spliterator are:
- Java Spliterator is an interface in Java Collection API.
- Spliterator is introduced in Java 8 release in
- It supports Parallel Programming functionality.
- We can use it for both Collection API and Stream API classes.
- It provides characteristics about Collection or API objects.
- We can NOT use this Iterator for Map implemented classes.
- It uses
tryAdvance()method to iterate elements individually in multiple Threads to support Parallel Processing.
- It uses
forEachRemaining()method to iterate elements sequentially in a single Thread.
- It uses
trySplit()method to divide itself into Sub-Spliterators to support Parallel Processing.
- Spliterator supports both Sequential and Parallel processing of data.
Spliterator itself does not provide the parallel programming behavior. However, it provides some methods to support it. Developers should utilize Spliterator interface methods and implement parallel programming by using Fork/Join Framework (one good approach).
Main Functionalities of Spliterator
- Splitting the source data.
- Processing the source data.
Java Spliterator Class Diagram
The following diagram shows the Class Diagram of Java Spliterator interface. It has many fields and methods.
Java Spliterator Methods
In this section, we will list out all Java Spliterator methods one by one with some useful description.
- int characteristics(): Returns a set of characteristics of this Spliterator and its elements.
- long estimateSize(): Returns an estimate of the number of elements that would be encountered by a forEachRemaining() traversal, or returns Long.MAX_VALUE if infinite, unknown, or too expensive to compute.
- default void forEachRemaining(Consumer action): Performs the given action for each remaining element, sequentially in the current thread, until all elements have been processed or the action throws an exception.
- default Comparator getComparator(): If this Spliterator’s source is SORTED by a Comparator, returns that Comparator.
- default long getExactSizeIfKnown(): Convenience method that returns estimateSize() if this Spliterator is SIZED, else -1.
- default boolean hasCharacteristics(int characteristics): Returns true if this Spliterator’s characteristics() contain all of the given characteristics.
- boolean tryAdvance(Consumer action): If a remaining element exists, performs the given action on it, returning true; else returns false.
- Spliterator trySplit(): If this spliterator can be partitioned, returns a Spliterator covering elements, that will, upon return from this method, not be covered by this Spliterator.
Java Spliterator Example
In this section, we will discuss about how to create Java Spliterator object using spliterator() and will develop simple example.
public class SpliteratorSequentialIteration
public static void main(String args)
List<String> names = new ArrayList<>();
// Getting Spliterator
Spliterator<String> namesSpliterator = names.spliterator();
// Traversing elements
<span style="color: #008000;"><strong><code>
If we observe the above program and output, we can easily understand that this Spliterator.forEachRemaining() method works in the same way as ArrayList.foreach(). Yes, both works in similar way.
Advantages of Spliterator
- Unlike Iterator and ListIterator, it supports Parallel Programming functionality.
- Unlike Iterator and ListIterator, it supports both Sequential and Parallel Processing of data.
- Compare to other Iterators, it provides better performance.
Iterator vs Spliterator
That’s all about Spliterator in Java.
Reference: API Doc