LinkedBlockingQueue iterator() method in Java
The iterator() method of LinkedBlockingQueue returns an iterator of the same elements, as this LinkedBlockingQueue, in a proper sequence. The elements returned from this method contains all the elements in order from first(head) to last(tail) of LinkedBlockingQueue. The returned iterator is weakly consistent.
Syntax:
public Iterator<E> iterator()
Return Value: The method returns the iterator having same elements as present in LinkedBlockingQueue in order from first(head) to last(tail).
Below programs illustrates iterator() method of LinkedBlockingQueue class:
Program 1: Creating an Iteretor from LinkedBlockingQueue which contains names of different student of a class.
// Java Program Demonstrate iterator() // method of LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue; import java.util.Iterator; 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("John"); linkedQueue.add("Tom"); linkedQueue.add("Clark"); linkedQueue.add("Kat"); // create Iterator of linkedQueue using iterator() method Iterator<String> listOfNames = linkedQueue.iterator(); // print result System.out.println("list of names:"); while (listOfNames.hasNext()) System.out.println(listOfNames.next()); } } |
list of names: John Tom Clark Kat
Program 2: Creating an Iteretor from LinkedBlockingQueue which contains list of Employees.
// Java Program Demonstrate iterator() // method of LinkedBlockingQueue import java.util.concurrent.LinkedBlockingQueue; import java.util.Iterator; public class GFG { public void collectIterator() { // 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("Sachin", "Developer", "39000"); Employee emp2 = new Employee("Sanjeev", "Tester", "26000"); // Add Employee Objects to linkedQueue linkedQueue.add(emp1); linkedQueue.add(emp2); // create Iterator of linkedQueue using iterator() method Iterator<Employee> listOfEmployee = linkedQueue.iterator(); // print result from iterator System.out.println("list of Employees:"); while (listOfEmployee.hasNext()) { System.out.println("*************************"); Employee emp = listOfEmployee.next(); System.out.println("Employee Name : " + emp.name); System.out.println("Employee Position : " + emp.position); System.out.println("Employee Salary : " + emp.salary); } } // create an Employee Object with name, // position and salary as an attribute 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.collectIterator(); } } |
list of Employees: ************************* Employee Name : Sachin Employee Position : Developer Employee Salary : 39000 ************************* Employee Name : Sanjeev Employee Position : Tester Employee Salary : 26000
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/LinkedBlockingQueue.html#iterator–
Recommended Posts:
- LinkedBlockingQueue contains() method in Java
- LinkedBlockingQueue remove() method in Java
- LinkedBlockingQueue peek() method in Java
- LinkedBlockingQueue poll() method in Java
- LinkedBlockingQueue toString() method in Java
- LinkedBlockingQueue size() method in Java
- LinkedBlockingQueue remainingCapacity() method in Java
- LinkedBlockingQueue clear() method in Java
- LinkedBlockingQueue put() method in Java with Examples
- LinkedBlockingQueue toArray() method in Java
- LinkedBlockingQueue drainTo() method in Java
- LinkedBlockingQueue take() Method in Java with Examples
- LinkedBlockingQueue | offer() Method in JAVA
- LinkedBlockingQueue toString() Method in Java with Examples
- Java 8 | LinkedBlockingQueue spliterator() method with Examples
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.



