Java ArrayList spliterator() Method Example

ArrayList spliterator() returns the instance of Spliterator which is last-binding and fail-fast. Java Spliterator is used for traversing and partitioning elements of a source such as an array, Set, List or IO channel.

Java ArrayList spliterator() Method Example

Java program to iterate one element at a time using spliterator. It is equivalent to iterator.next() method from the Iterator interface.
import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;

public class ArrayListDemo {

    public static void main(String[] args) {

        List < String > programmingLanguages = new ArrayList < > ();
        programmingLanguages.add("C");
        programmingLanguages.add("C++");
        programmingLanguages.add("Java");
        programmingLanguages.add("Kotlin");

        Spliterator < String > sItr = programmingLanguages.spliterator();

        sItr.tryAdvance(d - > System.out.println(d));
        sItr.tryAdvance(d - > System.out.println(d));
        sItr.tryAdvance(d - > System.out.println(d));
        sItr.tryAdvance(d - > System.out.println(d));
    }
}
Output:
C
C++
Java
Kotlin

Reference

Java ArrayList Source Code Examples


Comments