Java Utility Library

Java ArrayDeque - spliterator() Method



The java.util.ArrayDeque.spliterator() method is used to create a late-binding and fail-fast spliterator over the elements in the deque.

Syntax

public Spliterator<E> spliterator()

Here, E is the type of element maintained by the container.


Parameters

No parameter is required.

Return Value

Returns a spliterator over the elements in the deque.

Exception

NA.

Example:

In the example below, the java.util.ArrayDeque.spliterator() method is used to create a spliterator over the elements in the given deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating the ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(30);
    MyDeque.add(40);
    MyDeque.add(50);

    //creating spliterator object on the ArrayDeque 
    Spliterator<Integer> splitr = MyDeque.spliterator(); 

    //display content of the ArrayDeque 
    //using tryAdvance method
    System.out.print("MyDeque contains: ");              
    while(splitr.tryAdvance((n) -> System.out.print(n + " ")));   
  }
}

The output of the above code will be:

MyDeque contains: 10 20 30 40 50 

Example:

Lets consider another example to learn the concept of spliterator over the elements in the given deque.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating an ArrayDeque
    ArrayDeque<Integer> MyDeque = new ArrayDeque<Integer>();

    //populating the ArrayDeque
    MyDeque.add(10);
    MyDeque.add(20);
    MyDeque.add(-30);
    MyDeque.add(-40);
    MyDeque.add(-50);

    //creating spliterator object on the ArrayDeque 
    Spliterator<Integer> splitr = MyDeque.spliterator(); 

    //printing estimateSize of the ArrayDeque
    System.out.println("Estimated size: " + splitr.estimateSize());  

    //display content of the ArrayDeque
    //using forEachRemaining method
    System.out.print("MyDeque contains: ");               
    splitr.forEachRemaining((n) -> System.out.print(n + " "));
  }
}

The output of the above code will be:

Estimated size: 5
MyDeque contains: 10 20 -30 -40 -50 

❮ Java.util - ArrayDeque