How to use Java PriorityQueue with Examples

grokonez.com-java-priorityqueue-examples

In the tutorial, Grokonez introduces how to work with Java PriorityQueue by examples:

  • How does Java PriorityQueue work?
  • Explore Java PriorityQueue Hierarchy
  • Create Java PriorityQueue with Integer, String, and Custom Object Class

Related posts:
Java TreeMap with Examples

Java PriorityQueue

– Java PriorityQueue is an unbounded priority queue.
– The elements of the priority queue are ordered according to their natural ordering, or by a Comparator.

  • The front of the priority queue contains the least element.
  • And the rear of the priority queue contains the greatest element.

grokonez-howto-java-priority-queue-examples

– PriorityQueue does not permit null elements.
– PriorityQueue does not permit insertion of non-comparable objects. ClassCastException will be throwed.
– The head of this queue is the least element. The queue operations poll, remove, peek, and element access the element at the head of the queue.
– Java PriorityQueue is not synchronized. Multiple threads should not access the same PriorityQueue concurrently if having any of the threads modifies the queue.

Java PriorityQueue Hierarchy

grokonez-java-priority-queue-hierarchy-examples

Java PriorityQueue with Integer Example

In the follwing example, We create an Integer Java PriorityQueue, then do a list operations on the queue:

  • Insert an element to PriorityQueue with offer and add method.
  • Retrieve a head value of the Queue, but NOT remove it by peek() method.
  • Removes a single instance of the specified element from this queue by remove(Object o) method.
  • Retrieves and removes the head of this queue by poll() method.

Example:

– In above example, we use PriorityQueue() Constructor to create a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.

add() and offer() method, both can insert an element to a PriorityQueue, But what is the difference?
+ The offer method inserts an element if possible, otherwise returning false.
+ This differs from the Collection.add method, which can fail to add an element only by throwing an unchecked exception.

– The remove() and poll() methods remove and return the head of the queue.
-> BUT when the queue is empty, the remove() method throws an exception, while the poll() method returns null.

– The element() and peek() retrive the head’s value of the queue and NOT remove. When the queue is empty:
+ element() throws NoSuchElementException.
+ peek() returns null.

Now we do an example to iterate all elements of a Java PriorityQueue:

– using iterator(): to return an iterator over the elements in the queue.
– using toArray(): to return an array containing all the elements in the queue.

Java PriorityQueue with String Example

– Example: Create Java String PriorityQueue using Natural Ordering of String

Create Java PriorityQueue with a custom Comparator

– Create a Java PriorityQueue using a Custom Comparator that compares two Strings by their length:

-> Output:

Java PriorityQueue with Custom Object Example

– Create an Java Developer class:

– Implement Comparable interface to compare its elements and order them accordingly as below:

– Create a Java PriorityQueue Example with Developer class:

-> Output:

-> The Developer with smallest salary is removed first

Conclusion

We had learned how to work with Java PriorityQueue by examples:

  • Create Java PriorityQueue with Natural Ordering of Elements.
  • Create Java PriorityQueue with Custom Comparator.
  • Create Java PriorityQueue with Custom Object that implements Comparable interface.
  • Do a list operations of Java PriorityQueue: offer, peek, poll, remove

Happy Learning! See you later!

By grokonez | May 10, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*