ArrayBlockingQueue iterator() Method in Java
The iterator() method of ArrayBlockingQueue class is used to returns an iterator of the same elements as this queue in a proper sequence. The elements returned from this method contains elements in order from first(head) to last(tail). The returned iterator is weakly consistent.
Syntax:
public Iterator iterator()
Return Value: The method returns the iterator having same elements as present in ArrayBlockingQueue in proper sequence.
Below programs illustrates iterator() method of ArrayBlockingQueue class:
Program 1:
// Program Demonstrate how to apply iterator() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5; // Create object of ArrayBlockingQueue ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>(capacity); // Add 5 elements to ArrayBlockingQueue queue.offer(423); queue.offer(422); queue.offer(421); queue.offer(420); queue.offer(424); // Print queue System.out.println("Queue is " + queue); // Call iterator() method and Create an iterator Iterator iteratorValues = queue.iterator(); // Print elements of iterator System.out.println("\nThe iterator values:"); while (iteratorValues.hasNext()) { System.out.println(iteratorValues.next()); } } } |
Queue is [423, 422, 421, 420, 424] The iterator values: 423 422 421 420 424
Program 2:
// Program Demonstrate how to apply iterator() method // of ArrayBlockingQueue Class. import java.util.concurrent.ArrayBlockingQueue; import java.util.*; public class GFG { public static void main(String[] args) { // Define capacity of ArrayBlockingQueue int capacity = 5; // Create object of ArrayBlockingQueue ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(capacity); // Add 5 elements to ArrayBlockingQueue queue.offer("User"); queue.offer("Employee"); queue.offer("Manager"); queue.offer("Analyst"); queue.offer("HR"); // Print queue System.out.println("Queue is " + queue); // Call iterator() method and Create an iterator Iterator iteratorValues = queue.iterator(); // Print elements of iterator System.out.println("\nThe iterator values:"); while (iteratorValues.hasNext()) { System.out.println(iteratorValues.next()); } } } |
Queue is [User, Employee, Manager, Analyst, HR] The iterator values: User Employee Manager Analyst HR
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ArrayBlockingQueue.html#iterator
Recommended Posts:
- ArrayBlockingQueue put() method in Java
- ArrayBlockingQueue add() method in Java
- ArrayBlockingQueue take() method in Java
- ArrayBlockingQueue contains() method in Java
- ArrayBlockingQueue drainTo() Method in Java
- ArrayBlockingQueue poll() Method in Java
- ArrayBlockingQueue size() Method in Java
- ArrayBlockingQueue spliterator() method in Java
- ArrayBlockingQueue toString() Method in Java
- ArrayBlockingQueue peek() Method in Java
- ArrayBlockingQueue remove() method in Java
- ArrayBlockingQueue clear() Method in Java
- ArrayBlockingQueue offer() Method in Java
- ArrayBlockingQueue remainingCapacity() Method in Java
- ArrayBlockingQueue toArray() Method in Java
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.



