The spliterator() method of ArrayDeque returns a Spliterator of the same elements as ArrayDeque but created Spliterator is late-binding and fail-fast. A late-binding Spliterator binds to the source of elements means ArrayDeque 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 ArrayDeque.
Below programs illustrate spliterator() method of ArrayDeque:
Example 1: To demonstrate spliterator() method on ArrayDeque which contains a list of Numbers.
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<Integer> list = new ArrayDeque<Integer>();
list.add( 1234 );
list.add( 2345 );
list.add( 3456 );
list.add( 4567 );
Spliterator<Integer> numbers = list.spliterator();
System.out.println( "list of Numbers:" );
numbers.forEachRemaining((n) -> System.out.println(n));
}
}
|
Output:
list of Numbers:
1234
2345
3456
4567
Example 2: To demonstrate spliterator() method on ArrayDeque which contains list of Strings.
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<String> list = new ArrayDeque<String>();
list.add( "Kolkata" );
list.add( "Delhi" );
list.add( "Mumbai" );
list.add( "Jaipur" );
Spliterator<String> cities = list.spliterator();
System.out.println( "list of Cities:" );
cities.forEachRemaining(
(n) -> System.out.println( "City Name: " + n));
}
}
|
Output:
list of Cities:
City Name: Kolkata
City Name: Delhi
City Name: Mumbai
City Name: Jaipur
Reference:
https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#spliterator()
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!