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 the first traversal, first split, or the 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 a better way to traverse over element because it provides more control over elements.
Spliterator = Splitting + Iterator
- It uses tryAdvance() method to iterate elements individually in multiple Threads to support Parallel Processing,
- forEachRemaining() method to iterate elements sequentially in a single Thread,
- trySplit() method to divide itself into Sub-Spliterators to support Parallel Processing.
Spliterator supports both Sequential and Parallel processing of data. If you observe the output of below program’s output, you will find Spliterator.forEachRemaining() method works in the same way as ArrayList.foreach() but it provides better performance. 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.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
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" );
Spliterator<String> emails = list.spliterator();
System.out.println( "list of Emails:" );
emails.forEachRemaining(
(n) -> System.out.println(n));
}
}
|
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.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayList<user> list = new ArrayList<user>();
list.add( new user( "Aman" , 24 ));
list.add( new user( "Suraj" , 23 ));
list.add( new user( "Amar" , 24 ));
list.add( new user( "Kajal" , 22 ));
Spliterator<user> users = list.spliterator();
System.out.println( "list of Emails:" );
users.forEachRemaining((n) -> print(n));
}
public static void print(user u)
{
System.out.println( "User name : " + u.name
+ " and user age: " + u.age);
}
}
class user {
String name;
int age;
user(String name, int age)
{
this .name = name;
this .age = age;
}
}
|
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
Example 3:
Java
import java.io.*;
import java.util.ArrayList;
import java.util.Spliterator;
public class GFG {
public static void main(String[] args)
{
ArrayList<String> names = new ArrayList<>();
names.add( "Kumar" );
names.add( "Bob" );
names.add( "Raj" );
names.add( "David" );
names.add( "Eve" );
Spliterator<String> spliterator
= names.spliterator();
spliterator.forEachRemaining(System.out::println);
}
}
|
Output
Kumar
Bob
Raj
David
Eve
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Apr, 2023
Like Article
Save Article