English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about the PriorityQueue class of the Java collection framework through examples.
The PriorityQueue class provides the functionality of a heap data structure.
It implementsQueue interface
Unlike a regular queue, the elements of a priority queue are retrieved in sorted order.
Suppose we want to retrieve the elements in ascending order. In this case, the head of the priority queue is the smallest element. After retrieving this element, the next smallest element will become the head of the queue.
It should be noted that the elements of the priority queue may not be sorted. However, the elements are always retrieved in sorted order.
为了创建优先级队列,我们必须导入java.util.PriorityQueue包。导入程序包后,可以使用以下方法在Java中创建优先级队列。
PriorityQueue<Integer> numbers = new PriorityQueue<>();
To create a priority queue, we must import the java.util.PriorityQueue package. After importing the package, we can use the following methods to create a priority queue in Java.
Here, we create a priority queue with no parameters. In this case, the head of the priority queue is the smallest element in the queue. Elements will be removed from the queue in ascending order.
PriorityQueue methods
Insert elements into PriorityQueue - add()
Insert the specified element into the queue. If the queue is full, an exception will be thrown. - offer()
For example,
import java.util.PriorityQueue; class Main { public static void main(String[] args) { //Create a priority queue PriorityQueue<Integer> numbers = new PriorityQueue<>(); //Insert the specified element into the queue. If the queue is full, it returns false. numbers.add(4); numbers.add(2); System.out.println("PriorityQueue: ") + numbers);}} //Using the add() method Using the offer() method1); numbers.offer( + numbers);}} } }
Output Result
PriorityQueue: [2, 4] System.out.println("Updated PriorityQueue: ")1, 4, 2]
Updated PriorityQueue: [4And here, we have created a priority queue named numbers. We have already added2inserted into the queue.
Although4inserted into2Before that, but the head of the queue was2. This is because the head of the priority queue is the smallest element in the queue.
Then, we will1Now we rearrange the queue to place the smallest element1Stored at the front of the queue.
To access elements from the priority queue, we can use the peek() method. This method returns the head of the queue. For example,
import java.util.PriorityQueue; class Main { public static void main(String[] args) { // Create a priority queue PriorityQueue<Integer> numbers = new PriorityQueue<>(); numbers.add(4); numbers.add(2); numbers.add(1); System.out.println("PriorityQueue: ") + numbers);}} //Using the peek() method int number = numbers.peek(); System.out.println("Access element: ") + number); } }
Output Result
PriorityQueue: [1, 4, 2] Access element: 1
remove() - Delete the specified element from the queue
poll() - Return and delete the element at the front of the queue
For example,
import java.util.PriorityQueue; class Main { public static void main(String[] args) { // Create a priority queue PriorityQueue<Integer> numbers = new PriorityQueue<>(); numbers.add(4); numbers.add(2); numbers.add(1); System.out.println("PriorityQueue: ") + numbers);}} //Using the remove() method boolean result = numbers.remove(2); System.out.println("Element"2Has it been deleted? " + result); //Using the poll() method int number = numbers.poll(); System.out.println("The element deleted using poll(): ") + number); } }
Output Result
PriorityQueue: [1, 4, 2] Element2Has it been deleted? true The element deleted using poll(): 1
To traverse the elements of the priority queue, we can use the iterator() method. To use this method, we must import the java.util.Iterator package. For example,
import java.util.PriorityQueue; import java.util.Iterator; class Main { public static void main(String[] args) { //Create a priority queue PriorityQueue<Integer> numbers = new PriorityQueue<>(); numbers.add(4); numbers.add(2); numbers.add(1); System.out.print("Use iterator() to traverse PriorityQueue: \ "); //Use iterator() method Iterator<Integer> iterate = numbers.iterator(); while(iterate.hasNext()) { System.out.print(iterate.next()); System.out.print(", \ } } }
Output Result
Use iterator() to traverse PriorityQueue: 1, 4, 2,
Method | Content Description |
---|---|
contains(element) | Search for the specified element in the priority queue. If the element is found, return true; otherwise, return false. |
size() | Return the length of the priority queue. |
toArray() | Convert the priority queue to an array and return it. |
In all the above examples, the elements of the priority queue are retrieved in natural order (ascending). However, we can customize this order.
Therefore, we need to create our own comparator class, which implements the Comparator interface. For example
import java.util.PriorityQueue; import java.util.Comparator; class Main { public static void main(String[] args) { //Create a priority queue PriorityQueue<Integer> numbers = new PriorityQueue<>(new CustomComparator()); numbers.add(4); numbers.add(2); numbers.add(1); numbers.add(3); System.out.print("PriorityQueue: \" + numbers);}} } } class CustomComparator implements Comparator<Integer> { @Override public int compare(Integer number1, Integer number2) { int value = number1.compareTo(number2); //Elements are sorted in reverse order if (value > 0) { return -1; } else if (value < 0) { return 1; } else { return 0; } } }
Output Result
PriorityQueue: [4, 3, 1, 2]
In the above example, we created a priority queue and passed the CustomComparator class as a parameter.
The CustomComparator class implements the Comparator interface.
Then, we rewrite the compare() method. This method now makes the head of the element the largest number.