Java 8 | LinkedBlockingQueue spliterator() method with Examples

The spliterator() method of LinkedBlockingQueue returns a Spliterator of the same elements as LinkedBlockingQueue. The returned iterator is weakly consistent. It can be used with Streams in Java 8. Also it can traverse elements individually and in bulk too.
Syntax:Â
Â
public Spliterator spliterator()
Return Value: This method returns a Spliterator over the elements in LinkedBlockingQueue.
Below programs illustrates spliterator() method of LinkedBlockingQueue class:
Program 1: Creating a Spliterator from LinkedBlockingQueue which contains names of different student of a class
Â
Java
// Java Program Demonstrate spliterator()// method of LinkedBlockingQueueÂ
import java.util.concurrent.LinkedBlockingQueue;import java.util.*;public class GFG {Â
    public static void main(String[] args)    {        // define capacity of LinkedBlockingQueue        int capacityOfQueue = 7;Â
        // create object of LinkedBlockingQueue        LinkedBlockingQueue<String> linkedQueue            = new LinkedBlockingQueue<String>(capacityOfQueue);Â
        // Add element to LinkedBlockingQueue        linkedQueue.add("Aman");        linkedQueue.add("Amar");        linkedQueue.add("Sanjeet");        linkedQueue.add("Rabi");Â
        // create Spliterator of linkedQueue        // using spliterator() method        Spliterator<String> listOfNames = linkedQueue.spliterator();Â
        // print result from Spliterator        System.out.println("list of names:");Â
        // forEachRemaining method of Spliterator        listOfNames.forEachRemaining((n) -> System.out.println(n));    }} |
list of names: Aman Amar Sanjeet Rabi
Â
Program 2: Creating a Spliterator from LinkedBlockingQueue which contains list of Employees objects.
Â
Java
// Java Program Demonstrate spliterator()// method of LinkedBlockingQueueÂ
import java.util.concurrent.LinkedBlockingQueue;import java.util.*;public class GFG {Â
    public void collectSplitator()    {        // define capacity of LinkedBlockingQueue        int capacityOfQueue = 7;Â
        // create object of LinkedBlockingQueue        LinkedBlockingQueue<Employee> linkedQueue            = new LinkedBlockingQueue<Employee>(capacityOfQueue);Â
        // Add element to LinkedBlockingQueue        Employee emp1 = new Employee("Aman", "Blogger", "100000");        Employee emp2 = new Employee("Amar", "Manager", "99000");Â
        // Add Employee Objects to linkedQueue        linkedQueue.add(emp1);        linkedQueue.add(emp2);Â
        // create Spliterator of linkedQueue        // using spliterator() method        Spliterator<Employee> listOfEmp = linkedQueue.spliterator();Â
        // print result from Spliterator        System.out.println("No of Employees = "                           + linkedQueue.size());Â
        // forEachRemaining method of Spliterator        listOfEmp.forEachRemaining((n) -> print(n));    }Â
    // print employee details    public void print(Employee e)    {        System.out.println("-----------------------------");        System.out.println("Employee Name : " + e.name);        System.out.println("Employee Position : " + e.position);        System.out.println("Employee Salary : " + e.salary);    }Â
    // create an Employee Object with name,    // position and salary as attributes    public class Employee {Â
        public String name;        public String position;        public String salary;Â
        Employee(String name, String position, String salary)        {            this.name = name;            this.position = position;            this.salary = salary;        }Â
        @Override        public String toString()        {            return "Employee [name=" + name + ", position="                + position + ", salary=" + salary + "]";        }    }Â
    // Main Method    public static void main(String[] args)    {        GFG gfg = new GFG();        gfg.collectSplitator();    }} |
No of Employees = 2 ----------------------------- Employee Name : Aman Employee Position : Blogger Employee Salary : 100000 ----------------------------- Employee Name : Amar Employee Position : Manager Employee Salary : 99000
Â
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#spliterator–
Â



