How to Convert a PriorityQueue into an Array, List, and Set in Java?
Last Updated :
16 Feb, 2024
In Java, PriorityQueue is an implementation of the Queue interface. It means high-priority elements can be saved first compared to lower-priority elements in the queue, and it follows the natural order of elements.
In this article, we will learn how to convert a PriorityQueue into an Array, List, and Set in Java.
Approach to convert a PriorityQueue into an Array:
- First, create the class named GfGPriorityQueueToArray and define the main method.
- In the main method, create one integer priority queue using the PriorityQueue.
- Adding the elements of the two-priority queue.
- Using toArray() method to convert the priority queue to an array.
- Print the results array using a for loop traversal array and print the elements.
Program to Convert a PriorityQueue into an Array in Java
Java
import java.util.PriorityQueue;
public class GfGPriorityQueueToArray {
public static void main(String[] args) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add( 3 );
priorityQueue.add( 1 );
priorityQueue.add( 4 );
priorityQueue.add( 2 );
Integer[] array = priorityQueue.toArray( new Integer[ 0 ]);
System.out.println( "Array Elements are:" );
for (Integer element : array) {
System.out.println(element);
}
}
}
|
Output
Array Elements are:
1
2
4
3
Explanation of the Program:
The above Java program is the example of the converting the PriorityQueue into the array using toArray() method. Once it converted into an array, traverse the elements, and then print the elements.
- toArray(): This is a common method that can be used to convert another form object into the array object, and it’s returned the array objects.
Approach to convert a PriorityQueue into a List:
- First, create the class named GfGPriorityQueueToList and define the main method.
- In main method, create one integer priority queue using the PriorityQueue.
- Adding the elements of the two priority queues.
- Create the one list using List.
- After that add the priority queue into the list.
- Print the results list.
Program to Convert a PriorityQueue into a List in Java
Java
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
public class GfGPriorityQueueToList {
public static void main(String[] args) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add( 3 );
priorityQueue.add( 1 );
priorityQueue.add( 4 );
priorityQueue.add( 2 );
List<Integer> list = new ArrayList<>(priorityQueue);
System.out.println( "List Elements are: " +list);
}
}
|
Output
List Elements are: [1, 2, 4, 3]
Explanation of the Program:
- The above Java program is the example of the converting the PriorityQueue into the list using list interface.
- Once it converted into the list then print the resultant list.
Approach to convert a PriorityQueue into a Set:
- First, create the class named GfGPriorityQueueToSet and define the main method.
- In main method, create one integer priority queue using the PriorityQueue.
- Adding the elements of the two priority queues.
- Create the one set using Set interface.
- After that add the priority queue into the set.
- Then, print the resultant set.
Program to Convert a PriorityQueue into a Set in Java
Java
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;
public class PriorityQueueToSet {
public static void main(String[] args) {
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
priorityQueue.add( 3 );
priorityQueue.add( 1 );
priorityQueue.add( 4 );
priorityQueue.add( 2 );
Set<Integer> set = new HashSet<>(priorityQueue);
System.out.println( "Set elements are: " +set);
}
}
|
Output
Set elements are: [1, 2, 3, 4]
Explanation of the Program:
- The above Java program is the example of the converting the PriorityQueue into the set using Set Interface.
- Once it converted into the Set then print the resultant set.
Share your thoughts in the comments
Please Login to comment...