Java PriorityQueue.toArray()

Syntax

PriorityQueue.toArray() has the following syntax.

public Object [] toArray()

Example

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


/*  w  ww  . ja v  a2  s  .  c o m*/
import java.util.PriorityQueue;

public class Main {
   public static void main(String args[]) {

      PriorityQueue<Integer>   prq = new PriorityQueue<Integer>(); 
       
      // insert values in the queue
      prq.add(6);  
      prq.add(9);
      prq.add(5);
      prq.add(64);
      prq.add(6);
      
      System.out.println(prq);
      
      // get objects from the queue
      Object[] arr = prq.toArray(); 
  
      for ( int i = 0; i<arr.length; i++ ){  
         System.out.println (arr[i]) ; 
      }
   }
}

The code above generates the following result.