import java.util.*; public class QueueIteratorExample { public static void main(String[] args) { Queuequeue = new LinkedList<>(); queue.add(10); queue.add(20); queue.add(30); Iterator iterator = queue.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } } }
import java.util.*; public class QueueIteratorExample { public static void main(String[] args) { QueueIn this example, we create a Queue of String elements using LinkedList. We add 3 elements to this queue and then create an iterator using the iterator() method. We use a while loop to iterate over the queue and remove the element if its value is "Banana". We use the remove() method of the iterator to remove the element. Finally, we print the queue using System.out.println(). Package library: java.utilqueue = new LinkedList<>(); queue.add("Apple"); queue.add("Banana"); queue.add("Cherry"); Iterator iterator = queue.iterator(); while(iterator.hasNext()) { String element = iterator.next(); if(element.equals("Banana")) { iterator.remove(); } } System.out.println(queue); } }