PriorityQueue implementation principles and usage methods in Jin Collections
PriorityQueue implementation principles and usage methods in Jin Collections
Introduction to PriorityQueue
PriorityQueue is an important data structure in the Jin Collections library. It is a priority -based queue for storing a set of elements and accessed and operated according to their priority.In PriorityQueue, the elements are added to the queue according to a certain priority, and then the elements can be accessed and processed according to the order of priority from high to low or from low to high.
2. PriorityQueue implementation principle
The implementation principle of PriorityQueue is based on a binary pile data structure. It uses a complete binary tree to store data.In a complete binary tree, the priority of the parent node must be higher than or equal to the priority of its child node.
PriorityQueue of Jin Collections to achieve a binary structure by using a dynamic array.Use array storage elements at the bottom and re -sort according to the priority of the element.In PriorityQueue, the priority of the element is determined by the natural order of the element or the custom sorting rules specified by the comparator interface.
When adding elements to PriorityQueue, the element will be inserted into the end of the array, and then a series of floating operations. The upcoming elements will be compared with its parent node.Similarly, when the element is removed from the PriorityQueue, the elements located at the root node are first removed, then the elements at the end of the array are moved to the root node position, and a series of sinking operations.In comparison, if the priority is low, the position is exchanged until the right location is found.
By using the implementation of dynamic arrays and binary piles, PriorityQueue can have high efficiency when adding and removing elements. The time complexity is O (LOG N), where N is the number of elements in PriorityQueue.
Third, how to use PriorityQueue
It is very simple to use PriorityQueue in Jin Collections.First of all, we need to introduce the related classes of the Jin Collections library:
import com.jin.collections.PriorityQueue;
import com.jin.collections.Comparator;
Then, we can create a PriorityQueue object and specify the priority sorting rules of the element:
PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(Comparator.reverseOrder());
In this example, we created a PriorityQueue object that stored integers, and specified the rules for sorting of sorting through the `Comparator.reverseorder ()`).
Next, we can use the `add ()` method to add elements to the PriorityQueue:
priorityQueue.add(5);
priorityQueue.add(10);
priorityQueue.add(3);
You can obtain the highest priority element in PriorityQueue by calling the `Peek ()" method, and not to remove it:
int highestPriorityElement = priorityQueue.peek();
System.out.println (HighestpriorityElement); // output result is 10 as 10
We can also use the `Poll () method to obtain and remove the highest priority element in PriorityQueue:
int removedElement = priorityQueue.poll();
System.out.println (RemoveDelement); // The output result is 10
In addition, we can use the `size ()` method to obtain the number of elements in PriorityQueue:
int size = priorityQueue.size();
System.out.println (size); // The output result is 2
Summarize:
PriorityQueue in the Jin Collections library is a priority -based queue. Its implementation principle is based on binary pile data structure.Use PriorityQueue to access and operate elements in priority.By using the implementation of dynamic array and binary piles, PriorityQueue has high efficiency.When using PriorityQueue, we can specify the priority sorting rules of the element, and use the `ADD (),` Peek () and `Poll ()` to perform elements to add, obtain and remove operations.
The above is an introduction to the implementation principles and use methods of PriorityQueue in Jin Collection. I hope it will be helpful to you.