java.util.PriorityQueue.iterator() Method

Advertisements


Description

The iterator() method is used to return an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

Declaration

Following is the declaration for java.util.PriorityQueue.iterator() method.

public Iterator<E> iterator()

Parameters

  • NA

Return Value

  • The method call returns an iterator over the elements in this queue.

Exception

  • NA

Example

The following example shows the usage of java.util.PriorityQueue.iterator()

package com.tutorialspoint;

import java.util.*;

public class PriorityQueueDemo {
   public static void main(String args[]) {
      // create priority queue
      PriorityQueue < Integer >  prq = new PriorityQueue < Integer > (); 
       
      // insert values in the queue
      for ( int i = 0; i  <  10; i++ ){  
         prq.add (new Integer (i)) ; 
      }
      
      // create iterator from the queue
      Iterator it = prq.iterator();
      
      System.out.println ( "Priority queue values are: ");
      
      while (it.hasNext()){
         System.out.println ( "Value: "+ it.next()); 
      }
   }
}

Let us compile and run the above program, this will produce the following result.

Priority queue values are: 
Value: 0
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
Value: 6
Value: 7
Value: 8
Value: 9



Advertisements
Advertisements