ArrayList spliterator Method Tutorial

public Spliterator<E> spliterator()

The spliterator method returns a Spliterator object from the current ArrayList instance. What is a Spliterator? The answer is that a Spliterator is a far more advanced topic than where my tutorial series is thus far. I will be demonstrating how to use a spliterator in a future tutorial when I am on the topic of multi-threading. When you work with large amounts of data, you will strive to create the most efficient and speedy programs possible. Imagine that you have a custom class Box with instance variables of length, height, and width. Now picture that you have an ArrayList containing 5 billion Box objects and you need to find out the average volume of all Boxes in the ArrayList. You can easily loop through each element calculating the volume of each Box object, but that will take some time. The Spliterator interface contains methods for splitting up your ArrayList and running processes on multiple threads which will significantly increase the speed of calculating average volume. With that being said, I really cannot demonstrate something like that scenario because many of the concepts are beyond the scope of this tutorial. However, I will demonstrate a Spliterator iterating on a single thread using the Spliterator forEachRemaining(...) method which functions practically the same as the ArrayList forEach(...) method. If you haven't watched my ArrayList forEach Method Tutorial yet, I would highly recommend doing so.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ArrayListSpliterator
C:\Java>cd ArrayListSpliterator
C:\Java\ArrayListSpliterator>Notepad ArrayListSpliterator.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.util.*;
import java.util.function.*;
class ArrayListSpliterator {
    public static void main(String args[]) {
        ArrayList<Integer> intArray = new ArrayList<>();
        for(int i = 0; i <= 50; i++) {
            intArray.add(i * 2); // even numbers 0 to 100
        }
        System.out.println(intArray);

        Spliterator<Integer> mySpliterator = intArray.spliterator();
        System.out.println("mySpliterator.estimateSize() = " + mySpliterator.estimateSize());
        // watch my ArrayList forEach() tutorial for a detailed explanation on how a Consumer functional interface works.
        Consumer<Integer> c = x -> System.out.println("mySpliterator.forEachRemaining = " + x);
        mySpliterator.forEachRemaining(c);
        
    }
}

Now switch back to the command prompt (CMD) and type in javac ArrayListSpliterator.java and press Enter.
Now type in java ArrayListSpliterator and press Enter.


C:\Java\ArrayListSpliterator>javac ArrayListSpliterator.java
C:\Java\ArrayListSpliterator>java ArrayListSpliterator
[0, 2, 4, 6, 8, ... 96, 98, 100]
mySpliterator.estimateSize() = 51
mySpliterator.forEachRemaining = 0
mySpliterator.forEachRemaining = 2
mySpliterator.forEachRemaining = 4
mySpliterator.forEachRemaining = 6
...
mySpliterator.forEachRemaining = 96
mySpliterator.forEachRemaining = 98
mySpliterator.forEachRemaining = 100



Final thoughts

I would imagine that you may never have a need to use the spliterator method, but I figured I would go ahead and make this tutorial so you had an idea of what purpose it serves.


Tutorials