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