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 is the function of PriorityQueue?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

The main content of this article is to explain "what is the role of PriorityQueue", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the function of PriorityQueue"?

PriorityQueue uses the same as a normal queue, except that PriorityQueue determines who is at the head of the queue and who is at the end of the queue according to the collation.

Add a comparable object String to the queue:

Import java.util.PriorityQueue;public class Main {public static void main (String [] args) {PriorityQueue Q = new PriorityQueue (); / / listed q.add (3); q.add (5); q.add (10); q.add (7); q.add (9); q.add (15); q.add (11) Q.add (13); q.add (20); q.add (12); / / dequeued System.out.println (q.poll ()); / / 1 System.out.println (q.poll ()); / / 2 System.out.println (q.poll ()); / / 3 System.out.println (q.poll ()) / / 4 System.out.println (q.poll ()); / 5 System.out.println (q.poll ()); / / 1 System.out.println (q.poll ()); / / 2 System.out.println (q.poll ()); / / 3 System.out.println (q.poll ()); / 4 System.out.println (q.poll ()); / / 5}}

Observe the print result, and also make a judgment when you say the column, and return the minimum value. By default, PriorityQueue uses natural sorting, with the smallest element dequeued first.

Let's draw a picture to demonstrate and see:

Debug in idea to view variable information:

The heap array storage needs to be mastered, and the storage in the idea editor is the same as the actual drawing array structure.

At this point, I believe you have a deeper understanding of "what is the role of PriorityQueue". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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