Java ArrayList spliterator() Method
The spliterator() method in Java is used to iterate over the elements of an ArrayList. It provides a more efficient way to traverse the collection specially with parallel processing. This method is part of Java 8 and later.
Example 1: Here, we will use the spliterator() method to loop through elements in the ArrayList.
// loop through elements in an
// ArrayList using spliterator()
import java.util.ArrayList;
import java.util.Spliterator;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList
ArrayList<String> al = new ArrayList<>();
al.add("Apple");
al.add("Mango");
al.add("Banana");
al.add("Grapes");
// Get Spliterator
Spliterator<String> si = al.spliterator();
// Loop through elements
si.forEachRemaining(System.out::println);
}
}
Output
Apple Mango Banana Grapes
Explanation: In the above example, we have used the forEachRemaining() method of Spliterator to sequentially loop through all elements in an ArrayList.
Syntax of spliterator()
Method
public Spliterator<E> spliterator()
Return Value: This method returns a Spliterator over the elements in ArrayList.
Points to Remember:
- The spliterator() method of ArrayList returns a Spliterator of the same elements as ArrayList but created Spliterator is late-binding and fail-fast.
- A late-binding Spliterator binds to the source of elements. It means that Arraylist at the point of the first traversal, first split, or the first query for estimated size, rather than at the time the Spliterator is created.
- It can traverse elements individually and in bulk too. Spliterator is a better way to traverse over element because it provides more control over elements.
Spliterator = Splitting + Iterator
- It uses tryAdvance() method to iterate elements individually in multiple threads to support Parallel Processing,
- The forEachRemaining() method to iterate elements sequentially in a single thread,
- The trySplit() method is used to divide itself into sub-Spliterators to support Parallel Processing.
Example 2: Here, we will use the spliterator() method to perform Parallel Processing.
// Parallel Processing with splititerator()
import java.util.ArrayList;
import java.util.Spliterator;
public class GFG {
public static void main(String[] args) {
// Create an ArrayList and add numbers
ArrayList<Integer> n = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
n.add(i);
}
// Get a Spliterator and split it
// for parallel processing
Spliterator<Integer> si = n.spliterator();
Spliterator<Integer> si1 = si.trySplit();
// Parallel processing
si1.forEachRemaining(num -> System.out.println("Thread 1: " + num));
si.forEachRemaining(num -> System.out.println("Thread 2: " + num));
}
}
Output
Thread 1: 1 Thread 1: 2 Thread 2: 3 Thread 2: 4 Thread 2: 5
Explanation: The above example demonstrates parallel processing using Spliterator by splitting an ArrayList into two parts. The first Spliterator processes one part in “Thread 1,” and the second processes the remaining part in “Thread 2.”
Note: The actual output might vary depending on the JVM, as the division of elements between threads is not guaranteed to be exactly the same every time.