Java PriorityQueue.iterator()

Syntax

PriorityQueue.iterator() has the following syntax.

public Iterator <E> iterator()

Example

In the following code shows how to use PriorityQueue.iterator() method.


import java.util.Iterator;
import java.util.PriorityQueue;
/*w ww.  j  a  v a 2s. c  om*/
public class Main {
   public static void main(String args[]) {

      PriorityQueue<Integer>   prq = new PriorityQueue<Integer>(); 
       
      for ( int i = 0; i < 10; i++ ){  
         prq.add (i) ; 
      }
     
      // create iterator from the queue
      Iterator it = prq.iterator();
      
      while (it.hasNext()){
         System.out.println ( "Value: "+ it.next()); 
      }
   }
}

The code above generates the following result.