The Wayback Machine - https://web.archive.org/web/20240618020210/https://www.geeksforgeeks.org/arraydeque-spliterator-method-in-java/
Open In App

ArrayDeque spliterator() method in Java

Last Updated : 10 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.




// 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));
    }
}


Output:

list of Numbers:
1234
2345
3456
4567

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




// 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));
    }
}


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



Similar Reads

Java 8 | LinkedBlockingQueue spliterator() method with Examples
The spliterator() method of LinkedBlockingQueue returns a Spliterator of the same elements as LinkedBlockingQueue. The returned iterator is weakly consistent. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too.Syntax: public Spliterator spliterator() Return Value: This method returns a Spliterator over
3 min read
ArrayBlockingQueue spliterator() method in Java
The spliterator() method of ArrayBlockingQueue return a Spliterator on the elements of ArrayBlockingQueue. The returned iterator is weakly consistent. Spliterator can be used with Streams in Java 8. Spliterator can traverse elements individually and in bulk too.Syntax: public Spliterator spliterator() Return Value: This method returns a Spliterator
2 min read
PriorityBlockingQueue spliterator() method in Java
The spliterator() method of PriorityBlockingQueue returns a Spliterator of the same elements as PriorityBlockingQueue. The returned iterator is weakly consistent. 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 el
2 min read
LinkedTransferQueue spliterator() method in Java
The java.util.concurrent.LinkedTransferQueue.spliterator() method is an in-built function in Java which returns a weakly uniform Spliterator across the elements of this queue. Syntax: LinkedTransferQueue.spliterator() Parameters: The function does not accept any parameter. Return Value: The function returns a Spliterator across the elements of this
2 min read
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 the first traversal, first split, or the first query for estimated size, rather than at the time the
3 min read
LinkedBlockingDeque spliterator() method in Java
The spliterator() method of LinkedBlockingDeque returns a Spliterator on the elements of LinkedBlockingDeque. The returned iterator is weakly consistent. Spliterator can be used with Streams in Java 8. Spliterator can traverse elements individually and in bulk too. Syntax: public Spliterator spliterator() Returns: This method returns a Spliterator
2 min read
LinkedList spliterator() method in Java
The spliterator() method of LinkedList returns a Spliterator which is late-binding and fail-fast with the same elements as LinkedList. A late-binding Spliterator binds to the source of elements means LinkedList 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
3 min read
HashSet spliterator() method in Java
The spliterator() method of HashSet returns a Spliterator with the same elements as HashSet. The returned Spliterator is late-binding and fail-fast Spliterator. A late-binding Spliterator binds to the source of elements means HashSet at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliter
2 min read
ConcurrentSkipListSet spliterator() method in Java
The java.util.concurrent.ConcurrentSkipListSet.spliterator() method is an in-built function in Java which returns a weakly uniform Spliterator across the elements of this set. Syntax: ConcurrentSkipListSet.spliterator() Parameters: The function does not accept any parameter. Return Value: The function returns a Spliterator across the elements of th
2 min read
PriorityQueue spliterator() method in Java
The spliterator() method of PriorityQueue returns a Spliterator the same elements as PriorityQueue.The returned Spliterator is late-binding and fail-fast Spliterator. A late-binding Spliterator binds to the source of elements means PriorityQueue at the point of first traversal, first split, or first query for estimated size, rather than at the time
2 min read