PriorityQueue belongs to the Java Collections Framework. PriorityQueue is based on priority heap and it is an implementation of Queue interface. This data structure can be used when we need a Queue implementation and we have a requirement to maintain the elements of that collection in a specific sorted order based on each element’s priority. It was introduced in JDK 1.5
Following example explains how we can use a Java PriorityQueue collection.
package com.javapapers.java; import java.util.Comparator; import java.util.PriorityQueue; public class PriorityQueueExample { public static void main(String[] args) { Comparator<String> queueComparator = new VowelComparator(); PriorityQueue<String> priorityQueue = new PriorityQueue<String>(10, queueComparator); priorityQueue.add("orange"); priorityQueue.add("fig"); priorityQueue.add("watermelon"); priorityQueue.add("lemon"); while (priorityQueue.size() != 0) { System.out.println(priorityQueue.remove()); } } }
This Comparator class is used to determine the sort order of the above PriorityQueue.
package com.javapapers.java; import java.util.Comparator; class VowelComparator implements Comparator<String> { @Override public int compare(String x, String y) { if (getVowelCount(x) < getVowelCount(y)) { return -1; } else if (getVowelCount(x) > getVowelCount(y)) { return 1; } return 0; } public int getVowelCount(String word) { int vowel = 0; for (int i = 0; i < word.length(); i++) { char chr = word.charAt(i); if (chr == 'a' || chr == 'A' || chr == 'e' || chr == 'E' || chr == 'i' || chr == 'I' || chr == 'o' || chr == 'O' || chr == 'u' || chr == 'U') vowel++; } return vowel; } }
fig lemon orange watermelon
Comments are closed for "Java PriorityQueue".
Liked it….it was really good n easy to understand……thanks Joe.c
thank you
it is the best
Nice Example .. easy to understand .
Excellent information about the priority queue data structure.
I have already read about PriorityQueue in similar tutorial sites. Lot of people have already written about it.
Man, this is the ONLY TUTORIAL that gives depth and comprehensive information. Above all, it is simple to read and easy to understand.
Thanks for the author.
Good to know about this. Thanks Joe.
Nice introduction to PriorityQueue.
One observation: seems a typo.
while (priorityQueue.size() != 0) {
System.out.println(queue.remove());
}
should be
while (priorityQueue.size() != 0) {
System.out.println(priorityQueue.remove());
}
Nice explanation. Thanks Joe.
Hi Joe,
I guess, you still have to override equals() method of Comparator in VowelComparator class. Then only we could compile and run the example. Please correct me if I miss any point technically. Any way nice explanation.
Thanks Omkar. I have fixed the tutorial now.
Venky,
There were couple of issues in the example code.
1) mistake pointed by Omkar above. Some typo sneaked in :-(
2) The < and > symbols got eaten up by the browser rendering.
Both of these issues are fixed now in the example shown above. You can happily compile, run and see the output.
Thanks for the comment.
Thanks Abhishek.
Got. Thanks joe.
Very easy to understand. Thanks for posting.
Interesting post, thanks Joe
it’s easy to understand 。。。。