Priority Queue

PriorityQueue Class in Java with Programming Example

Priority QueueIn this chapter you will learn:

  1. What is PriorityQueue?
  2. Programming Example
 What is PriorityQueue?

Java PriorityQueue are unbounded queue based on priority heap. It store element according to their natural ordering and doesn’t allow null insertion. It is likely as random listing of element in a queue. Important points of PriorityQueue are as follow:

  • It is unbounded and based on Priority heap.
  • However it is unbounded still it has internal capacity to manage size of queue. Size of queue grows automatically if it reaches as maximum elements.
  • Order of elements gets defined based on constructors.
  • Methods, property and iterator are same as queue interface.

 

Constructors and Methods

Constructors Descriptions
PriorityQueue() Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.
PriorityQueue(Collection<? extends E> c) Creates a PriorityQueue containing the elements in the specified collection.
PriorityQueue(int initialCapacity) Creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.
PriorityQueue(int initialCapacity,
Comparator<? super E> comparator)
Creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.
PriorityQueue(PriorityQueue<? extends E> c) Creates a PriorityQueue containing the elements in the specified priority queue.
PriorityQueue(SortedSet<? extends E> c) Creates a PriorityQueue containing the elements in the specified sorted set.

Methods and Descriptions

Methods Descriptions
boolean add(E e) Inserts the specified element into this priority queue.
void clear() Removes all of the elements from this priority queue.
Comparator<? super E> comparator() Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.
boolean contains(Object o) Returns true if this queue contains the specified element.
Iterator<E> iterator() Returns an iterator over the elements in this queue.
boolean offer(E e) Inserts the specified element into this priority queue.
E peek() Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E poll() Retrieves and removes the head of this queue, or returns null if this queue is empty.
boolean remove(Object o) Removes a single instance of the specified element from this queue, if it is present.
int size() Returns the number of elements in this collection.
Object[] toArray() Returns an array containing all of the elements in this queue.
<T> T[] toArray(T[] a) Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.

Methods Inherited From Class

Class Methods
java.util.AbstractQueue addAll(), element(), remove()
java.util.AbstractCollection containsAll(), isEmpty(), removeAll(), retainAll(), toString()
java.lang.Object clone(), equals(), finalize(), getClass(), hashCode(), notify(), notifyAll(), wait(), wait(), wait()
java.util.Collection containsAll(), equals(), hashCode(), isEmpty(), removeAll(), retainAll()

Declaration:

PriorityQueue p=new PriorityQueue();

Programming Example

import java.util.*;
class PriorityQueue_Example
{
  public static void main(String[] args)
  {
    PriorityQueue p=new PriorityQueue();
    //Adding Element in PriorityQueue
    p.add("C#");
    p.add("Java");
    p.add("C++");    
    p.add("HTML");
    p.add("VB.Net");
    
    //Printing PriorityQueue
    Iterator itr=p.iterator();
    while(itr.hasNext())
    {
     System.out.print(itr.next()+" "); 
    }
    
    //Adding Elements
    p.add("PHP");
    System.out.println("\n" + p);
    
    //Accessing Top Element
    System.out.println("Top Element is "+ p.element());
    
    //Removing Top Element
    System.out.println("Item Removed : " + p.remove());
    
    //Prining PriorityQueue
    System.out.println(p);
    
  }
}

Output

D:\JavaProgram>javac PriorityQueue_Example.java
D:\JavaProgram>java PriorityQueue_Example

C# HTML C++ Java VB.Net
[C#, HTML, C++, Java, VB.Net, PHP]
Top Element is C#
Item Removed : C#
[C++, HTML, PHP, Java, VB.Net]
_

Summary

In this chapter you have learned about PriorityQueue in Java. It organizes their element order after removing each item. It is mostly used for random listing of elements in a queue. In the next chapter you will learn about Deque Class in Java.


Leave a Reply

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