ArrayList spliterator() returns the instance of Spliterator which is last-binding and fail-fast. Java Spliterator is used for traversing and partitioning elements of a source such as array
, Set
, List
or IO channel.
1. ArrayList spliterator() method
1.1. Method definition
In case of arraylist, spliterator()
method return the instance of ArrayListSpliterator
class which in inner class of ArrayList
and implements Spliterator
interface.
public Spliterator<E> spliterator() { return new ArrayListSpliterator<>(this, 0, -1, 0); } static final class ArrayListSpliterator<E> implements Spliterator<E> { //implementation }
Spliterator is fail-fast which means after the binding occurs, any interference with the elements is then detected, throwing a ConcurrentModificationException
.
1.2. Characteristics
The spliterator returned by ArrayList
is :
- ORDERED – indicates that the elements have a defined order when traversing and partitioning.
- SORTED – means that the elements follow a predefined sort order.
- SUBSIZED – indicates that this and any further resulting Spliterator are SIZED. Here SIZED means the Spliterator has been created from a source with a known size.
2. ArrayList spliterator() example
A Spliterator
can be used for many usecases. Few are usecases are discussed in below given examples.
2.1. tryAdvance() – Iterate one element at a time
Java program to iterate one element at a time using spliterator. It is equivalent to iterator.next() method from Iterator
interface.
import java.util.ArrayList; import java.util.Arrays; import java.util.Spliterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> digits = new ArrayList<>(Arrays.asList(1,2,3,4,5,6)); Spliterator<Integer> sItr = digits.spliterator(); sItr.tryAdvance( d -> System.out.println( d ) ); sItr.tryAdvance( d -> System.out.println( d ) ); sItr.tryAdvance( d -> System.out.println( d ) ); } }
Program output.
1 2 3
2.2. forEachRemaining() – Iterate all elements
Java program to iterate all elements and perform an action on them. It is equivalent to iterator.hasNext()
method along with iterator.next()
in a loop.
import java.util.ArrayList; import java.util.Arrays; import java.util.Spliterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> digits = new ArrayList<>(Arrays.asList(1,2,3,4,5,6)); Spliterator<Integer> sItr = digits.spliterator(); sItr.tryAdvance( d -> System.out.println( d ) ); //1 sItr.tryAdvance( d -> System.out.println( d ) ); //2 sItr.forEachRemaining( d -> System.out.println( d ) ); //3,4,5,6 } }
Program output.
1 2 3 4 5 6
2.3. trySplit() – Parallel processing
If you are working on concurrent application and list has large number of elements then it’s good idea to divide the list into two parts and process parallely.
trySplit()
method splits the current spliterator into two and returns the new one. The elements it is pointing to are divided into two equal lists.
Spliterator
is not thread safe, by default. It is responsibility of application code to create different threads and hand over the Spliterator
instances.import java.util.ArrayList; import java.util.Arrays; import java.util.Spliterator; public class ArrayListExample { public static void main(String[] args) { ArrayList<Integer> digits = new ArrayList<>(Arrays.asList(1,2,3,4,5,6)); Spliterator<Integer> sItr1 = digits.spliterator(); Spliterator<Integer> sItr2 = sItr1.trySplit(); System.out.println(sItr1.getExactSizeIfKnown()); //3 sItr1.forEachRemaining( d -> System.out.println( d ) ); //4,5,6 System.out.println("==========="); System.out.println(sItr2.getExactSizeIfKnown()); //3 sItr2.forEachRemaining( d -> System.out.println( d ) ); //1,2,3 } }
Program output.
3 4 5 6 =========== 3 1 2 3
3. Differences between Iterator and Spliterator
Iterator | Spliterator |
---|---|
Since Java 1.2. | Since Java 8. |
Can be used to iterate all collection classes. | Can be used to iterate array, stream, list and set. Not possible with map. |
Does not support parallel processing. | Supports parallel processing. |
That’s all for the ArrayList spliterator() method in Java.
Happy Learning !!
Read More:
A Guide to Java ArrayList
ArrayList Java Docs
Spliterator Java Docs
Comments