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 over the elements in LinkedBlockingDeque.

Below programs illustrate spliterator() method of LinkedBlockingDeque:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to demonstrate spliterator()
// method of LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<Integer> LBD
            = new LinkedBlockingDeque<Integer>();
  
        // Add elements
        LBD.add(22);
        LBD.add(34);
        LBD.add(45);
        LBD.add(67);
  
        // create Spliterator of Deque
        // using spliterator() method
        Spliterator<Integer> numbers = LBD.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 : 4
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 LinkedBlockingDeque
  
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create object of LinkedBlockingDeque
        LinkedBlockingDeque<String> LBD
            = new LinkedBlockingDeque<String>();
  
        // Add numbers to front of LinkedBlockingDeque
        LBD.add("Geeks");
        LBD.add("forGeeks");
        LBD.add("A");
        LBD.add("Computer");
        LBD.add("Portal");
  
        // create Spliterator of Deque
        // using spliterator() method
        Spliterator<String> numbers = LBD.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 : 5
list of Strings:
Geeks
forGeeks
A
Computer
Portal

Reference: <https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingDeque.html#spliterator–



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.