Java PriorityQueue toArray() Method



Description

The java PriorityQueue toArray() method returns an array containing all of the elements in this queue in proper sequence. The order in which elements are present in PriorityQueue is maintained in Array as well.

Declaration

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

public Object[] toArray()

Parameters

NA

Return Value

This method returns an array containing all of the elements in this queue.

Exception

NA

Java PriorityQueue toArray(T[] a) Method

Description

The Java PriorityQueue toArray(a) method returns an array containing all of the elements in this queue from first to last element). It returns the runtime type of the returned array as of the specified array. In case of queue fits in the specified array, it is returned. Otherwise, a new array is allocated with the specified runtime type and the size of current queue.

Declaration

Following is the declaration for java.util.PriorityQueue.toArray(T[] a) method

public <T> T[] toArray(T[] a)

Parameters

  • a − the array into which the elements of the queue are to be stored

Return Value

This method returns an array containing the elements of the queue.

Exception

ArrayStoreException − if the runtime type of the specified array is not a supertype of the runtime type of every element in this queue.

NullPointerException − if the specified array is null.

Getting Array of Ints from a PriorityQueue of Ints Example

The following example shows the usage of Java PriorityQueue toArray() method with Integers. We're creating an PriorityQueue of Integers, adding some elements, and then array is created using toArray() method. The array is iterated and elements are printed.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Integer> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(25);
      queue.add(30);
      queue.add(20);
      queue.add(18);        

      // get the array
      Object[] array = queue.toArray();

      for (Object object : array) {
         System.out.println(object);
      }
   }
}

Output

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

18
20
25
30

Getting Array of Strings from a PriorityQueue of Strings Example

The following example shows the usage of Java PriorityQueue toArray() method with Strings. We're creating an PriorityQueue of Strings, adding some elements, and then array is created using toArray() method. The array is iterated and elements are printed.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<String> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add("A");
      queue.add("B");
      queue.add("C");
      queue.add("D");        

      // get the array
      Object[] array = queue.toArray();

      for (Object object : array) {
         System.out.println(object);
      }
   }
}

Output

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

A
B
C
D

Getting Array of Objects from a PriorityQueue of Objects Example

The following example shows the usage of Java PriorityQueue toArray(T[]) method with Student objects. We're creating an PriorityQueue of Student objects, adding some elements, and then array is created using toArray(T[]) method. The array is iterated and elements are printed.

package com.tutorialspoint;

import java.util.PriorityQueue;

public class PriorityQueueDemo {
   public static void main(String[] args) {
      
      // create an empty priority queue
      PriorityQueue<Student> queue = new PriorityQueue<>();

      // use add() method to add elements in the queue
      queue.add(new Student(1, "Julie"));
      queue.add(new Student(2, "Robert"));
      queue.add(new Student(3, "Adam"));       

      // get the array of Student objects
      Student[] array = queue.toArray(new Student[queue.size()]);

      // print the array elements
      for (Student student : array) {
         System.out.println(student);
      }  
   }
}
class Student implements Comparable<Student> {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }

   @Override
   public int compareTo(Student student) {
      return this.rollNo - student.rollNo;
   }
}

Output

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

[ 1, Julie ]
[ 2, Robert ]
[ 3, Adam ]
java_util_priorityqueue.htm
Advertisements