Java PriorityQueue

Last modified on September 2nd, 2014 by Joe.

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

Java PriorityQueue

Java PriorityQueue Key points

PriorityQueue Example

Following example explains how we can use a Java PriorityQueue collection.

PriorityQueueExample.java

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());
		}
	}
}

VowelComparator.java

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;
	}
}

PriorityQueue Example Output

fig
lemon
orange
watermelon

Comments on "Java PriorityQueue"

  1. Ayan says:

    Liked it….it was really good n easy to understand……thanks Joe.c

  2. M.o. says:

    thank you
    it is the best

  3. Abhishek Kumar says:

    Nice Example .. easy to understand .

  4. Harish says:

    Excellent information about the priority queue data structure.

  5. Purnima says:

    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.

  6. suresh atta says:

    Good to know about this. Thanks Joe.

  7. Omkar says:

    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());
    }

  8. Vishakha says:

    Nice explanation. Thanks Joe.

  9. Venky says:

    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.

  10. Joe says:

    Thanks Omkar. I have fixed the tutorial now.

  11. Joe says:

    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.

  12. Joe says:

    Thanks Abhishek.

  13. Venky says:

    Got. Thanks joe.

  14. Mahesh V.Shet says:

    Very easy to understand. Thanks for posting.

  15. Vinuraj M V says:

    Interesting post, thanks Joe

  16. sunshine says:

    it’s easy to understand 。。。。

Comments are closed for "Java PriorityQueue".