ArrayList spliterator() method in java

Let’s learn ArrayList spliterator() method in java.

ArrayList spliterator() method in java

spliterator() method creates a late-binding and fail-fast Spliterator over the elements in this list.

Syntax:

public Spliterator<E> spliterator()

Returns:

a Spliterator over the elements in this list.

Also read – ArrayList in java

Now let’s see example on ArrayList spliterator() method.

import java.util.ArrayList;
import java.util.Spliterator;
public class ArrayListSpliteratorMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<String> al = new ArrayList<String>();
      al.add("red");
      al.add("blue");
      al.add("green");
      al.add("yellow");
      al.add("orange");
      // creating Spliterator of ArrayList and using spliterator() method
      Spliterator<String> iterate = al.spliterator();
      System.out.println("ArrayList of colors: ");
      // forEachRemaining method of Spliterator
      iterate.forEachRemaining((n) -> System.out.println(n));
   }
}


Output:

ArrayList of colors:
red
blue
green
yellow
orange