Java .spliterator()
In Java, a Spliterator is an interface introduced in Java 8 as part of the Stream API(Application Programming Interface). It provides a way to perform parallel traversal over the elements of a source, such as an array, a generator function, etc.
The ArrayList class in Java also implements the Spliterator interface using the .spliterator() method. This method returns a Spliterator over the elements in ArrayList. It is considered a better way to iterate over elements as it gives more control over them.
The .spliterator() method can be combined with a number of other methods:
.tryAdvance(): This method is used to iterate elements separately in several threads to enable parallel processing..forEachRemaining(): This method is used to iterate elements consecutively in a single thread..trySplit(): This method splits the obtainedSpliteratorinto separate parts to facilitate parallel processing.
Syntax
public Spliterator<T> split = list.spliterator()
Here, T represents the type of elements in the list collection. For example, if the collection is of type List<String>, i.e., it contains String elements, then T would be String.
Examples
Example 1
In this example, the .spliterator() method is called on the ArrayList instance named arrayList to obtain a Spliterator for its elements:
import java.util.ArrayList;import java.util.Spliterator;public class ArrayListSpliteratorExample {public static void main(String[] args) {// Creating an ArrayListArrayList<String> arrayList = new ArrayList<>();arrayList.add("Java");arrayList.add("Python");arrayList.add("C++");arrayList.add("JavaScript");// Obtaining a Spliterator from ArrayListSpliterator<String> spliterator = arrayList.spliterator();// // Iterating elements using the .forEachRemaining() methodspliterator.forEachRemaining(System.out::println);}}
The output of the mentioned code is as follows:
JavaPythonC++JavaScript
Example 2
In this example, the .trySplit() method is used to split the Spliterator into two parts. Then, the .forEachRemaining() method is used to traverse over the elements on each part one-by-one:
import java.util.ArrayList;import java.util.List;import java.util.Spliterator;public class ArrayListSpliteratorExample {public static void main(String[] args) {// Creating an ArrayListArrayList<String> arrayList = new ArrayList<>();arrayList.add("Ruby");arrayList.add("Rust");arrayList.add("PHP");arrayList.add("Go");// Obtaining a Spliterator from ArrayListSpliterator<String> spliterator = arrayList.spliterator();// Splitting the SpliteratorSpliterator<String> secondHalf = spliterator.trySplit();System.out.println("First Half:");// Iterating elements using the .forEachRemaining() methodspliterator.forEachRemaining(System.out::println);System.out.println("Second Half:");secondHalf.forEachRemaining(System.out::println);}}
The output of the mentioned code is as follows:
First Half:PHPGoSecond Half:RubyRust
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours