Java Program to Iterate Queue Elements Using the Iterator() Method

You are currently viewing Java Program to Iterate Queue Elements Using the Iterator() Method

Java program to iterate Queue elements using the iterator() method

// Java program to iterate Queue elements 
// using iterator() method

import java.util.*;

public class Main {
  public static void main(String[] args) {
    Queue < Integer > queue = new LinkedList < > ();

    queue.add(10);
    queue.add(20);
    queue.add(30);
    queue.add(40);
    queue.add(50);

    Iterator itr = queue.iterator();
    System.out.println("Queue elements: ");
    while (itr.hasNext()) {
      System.out.print(itr.next() + " ");
    }
  }
}

Output

Queue elements: 
10 20 30 40 50 

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply