Open In App
Related Articles

LinkedBlockingDeque spliterator() method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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:
// 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));
    }
}

                    
Output:
Size of Spliterator : 4
list of Numbers:
22
34
45
67
Program 2:
// 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));
    }
}

                    
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–

Last Updated : 26 Nov, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads