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:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
LBD.add( 22 );
LBD.add( 34 );
LBD.add( 45 );
LBD.add( 67 );
Spliterator<Integer> numbers = LBD.spliterator();
System.out.println( "Size of Spliterator : "
+ numbers.estimateSize());
System.out.println( "list of Numbers:" );
numbers.forEachRemaining((n) -> System.out.println(n));
}
}
|
Output:
Size of Spliterator : 4
list of Numbers:
22
34
45
67
Program 2:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
LinkedBlockingDeque<String> LBD
= new LinkedBlockingDeque<String>();
LBD.add( "Geeks" );
LBD.add( "forGeeks" );
LBD.add( "A" );
LBD.add( "Computer" );
LBD.add( "Portal" );
Spliterator<String> numbers = LBD.spliterator();
System.out.println( "Size of Spliterator : "
+ numbers.estimateSize());
System.out.println( "list of Strings:" );
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–
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
26 Nov, 2018
Like Article
Save Article