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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

list of names:
Aman
Amar
Sanjeet
Rabi

Program 2: Creating a Spliterator from LinkedBlockingQueue which contains list of Employees objects.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 emplyee 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();
    }
}

chevron_right


Output:

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–



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.