How to use the Priorityqueue priority queue in "Go" Java

Source: Internet
Author: User

The priority queue is another queue that differs from the FIFO queue. The element with the highest priority is removed from the queue each time.

Priorityqueue is a new data structure interface that is provided starting from JDK1.5.

If comparator is not provided, the elements in the priority queue are sorted by default in the natural order, that is, the numbers are small by default in the queue header, and the strings are sorted in dictionary order.

Because most of the information on the network will be priority queue each method attribute, there are few examples to explain, for the convenience of everyone later use, I wrote a demo~

If you want to implement a queue that is prioritized according to your wishes, you need to implement the comparator interface. The following method implements the establishment of a priority queue based on a variable.

ImportJava.util.Comparator;ImportJava.util.PriorityQueue;ImportJava.util.Queue; Public classTest {PrivateString name; Private intpopulation;  PublicTest (String name,intpopulation) {         This. Name =name;  This. Population =population; }     PublicString GetName () {return  This. Name; }     Public intgetpopulation () {return  This. Population; }     PublicString toString () {returnGetName () + "-" +getpopulation (); }     Public Static voidMain (String args[]) {Comparator<test> orderisdn =NewComparator<test>(){             Public intCompare (Test O1, test O2) {//TODO auto-generated Method Stub                intNumbera =o1.getpopulation (); intNumberb =o2.getpopulation (); if(Numberb >Numbera) {                    return1; }                Else if(numberb<Numbera) {                    return-1; }                Else                {                    return0;        }                        }                                }; Queue<test> Priorityqueue =NewPriorityqueue<test> (11, ORDERISDN); Test T1=NewTest ("T1", 1); Test T3=NewTest ("T3", 3); Test T2=NewTest ("T2", 2); Test T4=NewTest ("T4", 0);        Priorityqueue.add (t1);        Priorityqueue.add (T3);        Priorityqueue.add (T2);        Priorityqueue.add (T4);    System.out.println (Priorityqueue.poll (). toString ()); }}

How to use the Priorityqueue priority queue in "Go" Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.