PriorityQueue is an unbounded Queue implementation in Java, which is based on priority heap. PriorityQueue allows you to keep elements in a particular order, according to there natural order or custom order defined by Comparator interface in Java. Head of priority queue data structure will always contain least element with respect to specified ordering. For example, in this post, we will create a PriorityQueue of Items, which are ordered based upon there price, this will allow us to process Items, starting from lowest price. Priority queue is also very useful in implementing Dijkstra algorithm in Java. You can use to PriorityQueue to keep unsettled nodes for processing. One of the key thing to remember about PriorityQueue in Java is that it's Iterator doesn't guarantee any order, if you want to traverse in ordered fashion, better use Arrays.sort(pq.toArray()) method. PriorityQueue is also not synchronized, which means can not be shared safely between multiple threads, instead it's concurrent counterpart PriorityBlockingQueue is thread-safe and should be used in multithreaded environment. Priority queue provides O(log(n)) time performance for common enqueing and dequeing methods e.g. offer(), poll() and add(), but constant time for retrieval methods e.g. peek() and element().
How to use PriorityQueue in Java
Here is one example of using PriorityQueue in Java, as I said earlier, you can use PriorityQueue to consume elements in a particular order, which can be natural ordering or any custom ordering defined by Comparator provided to PriorityQueue. In this example, we have kept number of Items in PriorityQueue, whose natural ordering is decided by it's price. You can take a look at Item class compareTo method, it's consistent with equals method and also sorts Items based upon there price. I have also create Item as nested static class here. By storing Item in priority queue, you can always retrieve Item with lowest price by using poll() method of Queue interface.
From the above output, it's clear that PriorityQueue keeps least value element at head, where ordering is determined using compareTo() method. It doesn't keep all elements in that order though, only head being least value element is guaranteed. This is in fact main difference between TreeSet and PriorityQueue in Java, former keeps all elements in a particular sorted order, while priority queue only keeps head in sorted order. Another important point to note is that PriorityQueue doesn't permit null elements and trying to add null elements will result in NullPointerException, as shown below :
Like any other Collection class, it's worth noting to remember key points about PriorityQueue in Java.
1) PriorityQueue is not synchronized, if thread-safety is requirement use BlockingPriorityQueue.
2) Queue retrieval methods like poll(), peek() and element() access head of Queue, which keeps least element according to specified ordering.
3) Iterator returned by PriorityQueue doesn't offer any ordering traversal guarantee.
4) PriorityQueue doesn't allow null elements, if you try to add null, it will throw java.lang.NullPointerException.
That's all on what is PriorityQueue in Java and How to use them. It's an special class, which can be used in special scenarios, where you need to consume Orders in a particular order, remember BlockingQueue maintains insertion order, but if want to maintain a custom order, PriorityQueue or BlockingPriorityQueue is right collection to use. TreeSet also provides similar functionality, but doesn't have one short retrieval cum removal method e.g. poll().
Though PriorityQueue has quite specialized use, it is one of the Collection class, which is often overlooked. Like any other Collection topic, Java Generics and Collection contains some of the best available information about PriorityQueue. If you like to learn more, take a copy of that book.