Java Priority queue some problems

Source: Internet
Author: User

No matter how the initialization, the length of the priority queue is not fixed, you add one, size will be + +, full will be expanded.

Therefore, you must manually maintain the priority queue size, such as the TOPK problem.

When you define a Len,len<max, you add,len++ directly.

After the poll is full, add

Put a bunch of unordered elements into the priority queue, each call to poll, can get the current small top heap, that is, the minimum value inside.

The priority queue is the small top heap by default, and you can pass in the sorting method yourself. It would be nice to spread the opposite order on the big top heap.

//I define the class Public Static classnode{intcap; intPro;  PublicNode (intXinty) {Cap=x; Pro=y; }}priorityqueue<Node> Priorityqueue =NewPriorityqueue<> (NewComparator<node>() {@Override Public intCompare (node O1, node O2) {if(o1.pro>O2.pro)return-1; if(o1.pro<O2.pro)return1; return0; }        });

Lamda expressions can also be

Without the new Comxxxx class, the direct transfer function can be

The parameter type does not need to fill, will fill you according to the context.

New Priorityqueue<> ((O1,O2),{                if(o1.pro>o2.pro)                    return -1 ;                 if (o1.pro<o2.pro)                     return 1;                 return 0;        });

Java Priority queue some problems

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.