Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does PriorityQueue priority queue mean?

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report