Skip to content
Related Articles

Related Articles

ConcurrentLinkedDeque Spliterator() method in Java with Examples
  • Last Updated : 27 Dec, 2018

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:






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

Program 2:




// 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));
    }
}
Output:
Size of Spliterator : 9223372036854775807
list of Strings:
Geeks
forGeeks
A
Computer
Portal

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :