ArrayList spliterator() method in Java

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 first traversal, first split, or first query for estimated size, rather than at the time the Spliterator is created. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too. Spliterator is better way to traverse over element because it provides more control on elements.

Syntax:

public Spliterator<E> spliterator()

Returns: This method returns a Spliterator over the elements in ArrayList.

Below programs illustrate spliterator() method of ArrayList:

Example 1: To demonstrate spliterator() method on ArrayList which contains a list of emails.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of ArrayList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayList which contains
        // emails for a group of people
        ArrayList<String> list = new ArrayList<String>();
  
        // Add emails to list
        list.add("abc@geeksforgeeks.org");
        list.add("user@geeksforgeeks.org");
        list.add("pqr@geeksforgeeks.org");
        list.add("random@geeksforgeeks.org");
        list.add("randomuser@geeksforgeeks.org");
  
        // create Spliterator of ArrayList
        // using spliterator() method
        Spliterator<String> emails = list.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Emails:");
  
        // forEachRemaining method of Spliterator
        emails.forEachRemaining((n) -> System.out.println(n));
    }
}

chevron_right


Output:

list of Emails:
abc@geeksforgeeks.org
user@geeksforgeeks.org
pqr@geeksforgeeks.org
random@geeksforgeeks.org
randomuser@geeksforgeeks.org

Example 2: To demonstrate spliterator() method on ArrayList which contains list of Users.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of ArrayList
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayList which contains
        // user details from user class
        ArrayList<user> list = new ArrayList<user>();
  
        // Add emails to list
        list.add(new user("Aman", 24));
        list.add(new user("Suraj", 23));
        list.add(new user("Amar", 24));
        list.add(new user("Kajal", 22));
  
        // create Spliterator of ArrayList
        // using spliterator() method
        Spliterator<user> users = list.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Emails:");
  
        // forEachRemaining method of Spliterator
        users.forEachRemaining((n) -> print(n));
    }
  
    public static void print(user u)
    {
        System.out.println("User name : " + u.name
                           + " and user age: " + u.age);
    }
}
  
// create a user class
class user {
  
    String name;
    int age;
  
    user(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
}

chevron_right


Output:

list of Emails:
User name : Aman and user age: 24
User name : Suraj and user age: 23
User name : Amar and user age: 24
User name : Kajal and user age: 22

Reference:
https://docs.oracle.com/javase/10/docs/api/java/util/ArrayList.html#spliterator()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.