6.) Priority Queue (basic)
Write a program to convert a priority queue to an array.
import java.util.PriorityQueue;
public class PriorityQueueToArray {
public static void main(String[] args) {
// Create a priority queue
PriorityQueue<String> pq = new PriorityQueue<String>();
// Add elements to the queue
pq.add("Apple");
pq.add("Banana");
pq.add("Grapes");
pq.add("Orange");
// Convert priority queue to an array
String[] arr = pq.toArray(new String[pq.size()]);
// Print the elements of the array
for (String s : arr) {
System.out.print(s + " ");
}
}
}
Comments
Post a Comment