ArrayDeque spliterator() method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of numbers
        ArrayDeque<Integer> list = new ArrayDeque<Integer>();
  
        // Add numbers to list
        list.add(1234);
        list.add(2345);
        list.add(3456);
        list.add(4567);
  
        // create Spliterator of ArrayDeque
        // using spliterator() method
        Spliterator<Integer> numbers = list.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Numbers:");
  
        // forEachRemaining method of Spliterator
        numbers.forEachRemaining((n) -> System.out.println(n));
    }
}

chevron_right


Output:

list of Numbers:
1234
2345
3456
4567

Example 2: To demonstrate spliterator() method on ArrayDeque which contains list of Strings.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate spliterator()
// method of ArrayDeque
  
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Strings
        ArrayDeque<String> list = new ArrayDeque<String>();
  
        // Add Strings to list
        // each string represents city name
        list.add("Kolkata");
        list.add("Delhi");
        list.add("Mumbai");
        list.add("Jaipur");
  
        // create Spliterator of ArrayDeque
        // using spliterator() method
        Spliterator<String> cities = list.spliterator();
  
        // print result from Spliterator
        System.out.println("list of Cities:");
  
        // forEachRemaining method of Spliterator
        cities.forEachRemaining(
            (n) -> System.out.println("City Name: " + n));
    }
}

chevron_right


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()



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.