In this tutorial, we will learn about the Java ArrayList spliterator() method, and learn how to use this method to get Spliterator for the elements in this ArrayList, with the help of examples.

Java ArrayList spliterator() method

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

Syntax

The syntax of spliterator() method is

ArrayList.spliterator()

Returns

The method returns Spliterator<E> object.

ADVERTISEMENT

1. spliterator() – Create Spliterator for the ArrayList

In this example, we will take an ArrayList of strings and get Spliterator over the elements in this ArrayList using ArrayList spliterator() method.

Java Program

import java.util.ArrayList;
import java.util.Spliterator;

public class Example {
  public static void main(String[] args) {
	  ArrayList<String> arrayList = new ArrayList<String>();  
	  arrayList.add("a");  
	  arrayList.add("b");  
	  arrayList.add("c"); 
	  arrayList.add("d");
	  arrayList.add("e");
	  
      Spliterator<String> spliterator = arrayList.spliterator();
      spliterator.forEachRemaining((str) -> System.out.println(str));
  }
}

Output

a
b
c
d
e

Conclusion

In this Java Tutorial, we have learnt the syntax of Java ArrayList spliterator() method, and also learnt how to use this method with the help of Java example programs.