ConcurrentLinkedDeque Spliterator() method in Java with Examples

The spliterator() method of ConcurrentLinkedDeque returns a Spliterator on the elements of ConcurrentLinkedDeque. 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 over the elements in ConcurrentLinkedDeque.

Below programs illustrate spliterator() method of ConcurrentLinkedDeque:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate spliterator()
// method of ConcurrentLinkedDeque
  
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque<Integer> CBD
            = new ConcurrentLinkedDeque<Integer>();
  
        // Add elements
        CBD.add(22);
        CBD.add(34);
        CBD.add(45);
        CBD.add(67);
  
        // create Spliterator of Deque
        // using spliterator() method
        Spliterator<Integer> numbers
            = CBD.spliterator();
  
        // getExactSize of Spliterator
        System.out.println("Size of Spliterator : "
                           + numbers.estimateSize());
  
        System.out.println("list of Numbers:");
  
        // forEachRemaining method of Spliterator
        numbers.forEachRemaining(
            (n) -> System.out.println(n));
    }
}

chevron_right


Output:

Size of Spliterator : 9223372036854775807
list of Numbers:
22
34
45
67

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate spliterator()
// method of ConcurrentLinkedDeque
  
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of ConcurrentLinkedDeque
        ConcurrentLinkedDeque<String> CBD
            = new ConcurrentLinkedDeque<String>();
  
        // Add numbers to front of ConcurrentLinkedDeque
        CBD.add("Geeks");
        CBD.add("forGeeks");
        CBD.add("A");
        CBD.add("Computer");
        CBD.add("Portal");
  
        // create Spliterator of Deque
        // using spliterator() method
        Spliterator<String> numbers
            = CBD.spliterator();
  
        // getExactSize of Spliterator
        System.out.println("Size of Spliterator : "
                           + numbers.estimateSize());
  
        System.out.println("list of Strings:");
  
        // forEachRemaining method of Spliterator
        numbers.forEachRemaining(
            (n) -> System.out.println(n));
    }
}

chevron_right


Output:

Size of Spliterator : 9223372036854775807
list of Strings:
Geeks
forGeeks
A
Computer
Portal


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.