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