Priority queue of Java

Source: Internet
Author: User

Priorityqueue belongs to the Java collections Framework. Priorityqueue is based on the priority heap, which is the implementation of the queue interface. when we need a queue implementation, we can use this data structure,

and we need to maintain the elements of the collection in a particular sort order based on the priority of each element. It was introduced in JDK 1.5.

Java Priority Queue Essentials

    • When instantiating Priorityqueue, you can provide a comparer in the constructor. The order of the items in the queue is then determined by the comparer that is provided.
    • If you do not provide a comparer, the elements are sorted using the natural order of collection (comparable).
    • This collection is not allowed to be null. The
    • queue leader is the fewest item in the sort. The sort relationship between the
    • priorityqueue elements is arbitrary.
    • priorityqueue is not synchronized. The Priorityblockingqueue is a thread-safe correspondence for Priorityqueue. The
    • priorityqueue is unbounded, and it dynamically grows based on the number of elements in the queue. It has internal capabilities at any given time and increases as elements are added. This internal capacity and increment policy is not specified or standardized.
    • this priorityqueue iterator () does not guarantee that the queue element is traversed in any particular order.
    • behaves wisely   Remove () and contains () methods take linear time. Peek (), Element (), and size () require a fixed time. Offer (), poll () and remove () Take O (log n) time.
    • offer () and add () are methods of the queue interface, implemented by Priorityqueue. These are used to insert elements into the queue. They have the same performance in Priorityqueue, no difference.
Priorityqueue Example

The following example shows how to use the Java Priorityqueue collection.

Priorityqueueexample.java
 PackageCom.javapapers.java;ImportJava.util.Comparator;ImportJava.util.PriorityQueue; Public classPriorityqueueexample { Public Static voidMain (string[] args) {Comparator<String> Queuecomparator =NewVowelcomparator (); Priorityqueue<String> Priorityqueue =NewPriorityqueue<string> (10, Queuecomparator); Priorityqueue.add ("Orange"); Priorityqueue.add ("Fig"); Priorityqueue.add ("Watermelon"); Priorityqueue.add ("Lemon");  while(Priorityqueue.size ()! = 0) {System.out.println (Priorityqueue.remove ()); }    }}

Vowelcomparator.java

This comparator class is used to determine the sort order of the above priorityqueue.

 PackageCom.javapapers.java;ImportJava.util.Comparator;classVowelcomparatorImplementsComparator<string>{@Override Public intCompare (string x, string y) {if(Getvowelcount (x) <Getvowelcount (y)) {            return-1; } Else if(Getvowelcount (x) >Getvowelcount (y)) {            return1; }        return0; }     Public intGetvowelcount (String word) {intVowel = 0;  for(inti = 0; I < word.length (); i++) {            CharCHR =Word.charat (i); ifChr = = ' a ' | | chr = = ' a ' | | chr = = ' E ' | | chr = ' e ' | | chr = = ' I ' | | chr = = ' I ' | | = = ' O ' | | Chr = = ' U ' | | Chr = = ' U ') Vowel++; }        returnvowel; }}

Priorityqueue Sample output:
FigLemonOrangeWatermelon

http://javapapers.com/java/search-file-using-nio/

Priority queue of Java

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.