Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What does PriorityQueue priority queue mean?

Shulou Source: shulou.com Published: 2022-06-02 08:13:05 10月24日 Update

This article mainly explains "what is the meaning of PriorityQueue priority queue". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what PriorityQueue priority queue means".

The JDK version is 1.8.

Feature 1. Cannot insert null object 2. The inserted object supports sorting. (you can pass in the comparator yourself) 3. If the results of the sort are the same, then the positions of the two objects in the queue are 4. 5% random. The queue is unbounded 5. Thread is not safe, if safe, please use PriorityBlockingQueue6. Is a complete binary tree to achieve the minimum binary heap (the value of non-child nodes, not greater than its left and right child nodes), so use an array for storage (the relationship between parent and child nodes can be calculated by formulas). Main methods 1. Add () and offer () insertion methods

There is no essential difference between the two methods, add () just wraps the offer () method.

If (e = = null) throw new NullPointerException (); modCount++; int I = size; if (I > = queue.length) grow (I + 1); / / capacity expansion size = I + 1; if (I = = 0) queue [0] = e queue / team empty, directly put into else siftUp (I, e) / / adjust the node (this method is the main method), insert e at position I, and compare it with the parent node of e until the node is greater than or equal to its parent or root. Return true;2.peek () gets the element

Gets the queue header element, but does not delete it

(size = = 0)? Null: (e) queue [0]; 3.poll () get the element

Gets the queue header element, but deletes the header element, and because the header element is deleted, the element position of the queue is adjusted

If (size = = 0) return null; int s =-- size; / / queue size minus one modCount++; E result = (E) queue [0]; / / get header element E x = (E) queue [s]; / / record queue last element queue [s] = null / / delete the last element of the queue if (s! = 0) siftDown (0, x); / / adjust the position of the element of the entire queue, insert the item x at position 0 and compare it with the leaf node of x until it is less than or equal to its child or leaf return result Application of Top K algorithm solutions (specific search related materials) Thank you for reading, the above is "what is the meaning of PriorityQueue priority queue" content, after the study of this article, I believe that you have a deeper understanding of what PriorityQueue priority queue means, the specific use of the need for you to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Queue element node method position priority meaning object learning adjustment security content leaf situation sort minimum two formula just can be passed through Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Docker MariaDB Xiaomi vpn