PriorityQueue Class

PriorityQueue extends AbstractQueue and implements the Queue interface.

It creates a queue that is prioritized based on the queue's comparator. PriorityQueue is a generic class that has this declaration:

class PriorityQueue<E>

E specifies the type of objects stored in the queue.

PriorityQueues are dynamic, growing as necessary. PriorityQueue defines the six constructors shown here:

PriorityQueue( )
builds an empty queue. Its starting capacity is 11.
PriorityQueue(int capacity)
builds a queue that has the specified initial capacity.
PriorityQueue(int capacity, Comparator<? super E> comp)
builds a queue with the specified capacity and comparator.
PriorityQueue(Collection<? extends E> c)
initialized with the collection passed.
PriorityQueue(PriorityQueue<? extends E> c)
initialized with the collection passed.
PriorityQueue(SortedSet<? extends E> c)
initialized with the collection passed.

If no comparator is specified when a PriorityQueue is constructed, then the default comparator is used. The default comparator will order the queue in ascending order.

Thus, the head of the queue will be the smallest value. By providing a custom comparator, you can specify a different ordering scheme.

PriorityQueue comparator( ) method return a reference to the comparator:

Comparator<? super E> comparator( )

If natural ordering is used for the invoking queue, null is returned.

To properly use a PriorityQueue, you must call methods such as offer( ) and poll( ), which are defined by the Queue interface.

Adding randomly generated integers to a priority queue

 
import java.util.PriorityQueue;
import java.util.Queue;

public class Main {
  public static void main(String[] args) {
    Queue<Integer> qi = new PriorityQueue<Integer>();
    for (int i = 0; i < 15; i++){
      qi.add((int) (Math.random() * 100));
    }
      
    while (!qi.isEmpty()){
      System.out.print(qi.poll() + " ");
    }
      
    System.out.println();
  }
}
  

17 25 36 36 37 42 44 46 48 55 66 70 77 82 98

Using a comparator with a priority queue

 
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class Main {
  final static int Count = 15;

  public static void main(String[] args) {
    Comparator<Integer> cmp;
    cmp = new Comparator<Integer>() {
      public int compare(Integer e1, Integer e2) {
        return e2 - e1;
      }
    };
    Queue<Integer> qi = new PriorityQueue<Integer>(Count, cmp);
    for (int i = 0; i < Count; i++){
      qi.add((int) (Math.random() * 100));
    }
      
    while (!qi.isEmpty()){
      System.out.print(qi.poll() + " ");
    }
      
    System.out.println();
  }
}
  

98 96 92 74 71 70 54 37 37 34 20 18 14 9 7

 
import java.util.Comparator;
import java.util.PriorityQueue;

enum ProductQuality {
  High, Medium, Low
}

class Product implements Comparable<Product> {
  String name;

  ProductQuality priority;

  Product(String str, ProductQuality pri) {
    name = str;
    priority = pri;
  }

  public int compareTo(Product msg2) {
    return priority.compareTo(msg2.priority);
  }
}

class MessageComparator implements Comparator<Product> {
  public int compare(Product msg1, Product msg2) {
    return msg2.priority.compareTo(msg1.priority);
  }
}

public class Main {
  public static void main(String args[]) {

    PriorityQueue<Product> pq = new PriorityQueue<Product>(3);

    pq.add(new Product("A", ProductQuality.Low));
    pq.add(new Product("B", ProductQuality.High));
    pq.add(new Product("C", ProductQuality.Medium));
    Product m;
    while ((m = pq.poll()) != null)
      System.out.println(m.name + " Priority: " + m.priority);

    PriorityQueue<Product> pqRev = new PriorityQueue<Product>(3, new MessageComparator());

    pqRev.add(new Product("D", ProductQuality.Low));
    pqRev.add(new Product("E", ProductQuality.High));
    pqRev.add(new Product("F", ProductQuality.Medium));

    while ((m = pqRev.poll()) != null)
      System.out.println(m.name + " Priority: " + m.priority);
  }
}
  
Home 
  Java Book 
    Collection