Queue:

  1. 1.5Version enhancements (Queue interface)
  2. It is the child interface of Collection
  3. If we want to represent group of individual objects prior to processing then we should go for Queue.
  4. For Example Before sending SMS messages all mobile numbers we have to store in some data structure. In which we ordered mobile numbers in the same order only message should be delivered. For this First In First Out requirement Queue is best choice.
  5. Usually, Queue follows First In First Out order. Based on our requirement we can implement our own priority order also. (Priority Queue).
  6. From 1.5 version also LinkedList class implements Queue interface.
  7. LinkedList based implementation of the Queue always follows First In First Out order.

Queue interface specific methods:

boolean offer(Object o)

to add an object into the Queue

Object peek()

to return head element of the queue. If the queue is empty then this method returns null.

Object element()

to return head element of the queue. If the queue is empty then this method raises RE: NoSuchElementException

Object poll()

to remove and return head element of the queue. If the queue is empty then this method returns null

Object remove()

to remove and return head element of the queue. If the queue is empty then this method raises RE: NoSuchElementException

PriorityQueue:

  1. If we want to represent a group of individual objects prior to processing according to some priority then we should go for PriorityQueue.
  2. The priority can be either default natural sorting order or customized sorting order defined by Comparator.
  3. Insertion order is not preserved and it is based on some priority
  4. Duplicate objects are not allowed
  5. If we are depending on default natural sorting order compulsory the object should be homogeneous and Comparable otherwise, we will get runtime exception saying ClassCastException.
  6. If we are defining our own sorting by Comparator then objects need not be homogenous and Comparable.
  7. Null is not allowed even as a first element also.

Constructors:

public PriorityQueue()

creates an empty PriorityQueue with default initial capacity 11 and all objects will be inserted according to default natural sorting order.

public PriorityQueue(int initialCapacity)
public PriorityQueue(int initialCapacityComparator c)
public PriorityQueue(SortedSet s)
public PriorityQueue(Collection c)
//Program using PriorityQueue
import java.util.*;
class PriorityQueueDemo{
    public static void main(String[] args){
        PriorityQueue pq = new PriorityQueue();
        System.out.println(pq.peek()); //null
        //System.out.println(q.element()); //RE: NoSuchElementException
        for(int i = 0; i <= 10; i++){
            pq.offer(i);
        }
        System.out.println(pq); //[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        System.out.println(pq.poll()); //0
        System.out.println(pq); //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    }
}

Output:
null
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
0
[1, 3, 2, 7, 4, 5, 6, 10, 8, 9]
Note: Some platforms won’t provide proper support for Thread priorities and PriorityQueues.


import java.util.*;
class PriorityQueueDemo{
    public static void main(String[] args){
        PriorityQueue pq = new PriorityQueue(15new MyComparator());
        pq.offer("kalyan");
        pq.offer("chanukya");
        pq.offer("hemanth");
        pq.offer("siva");
        System.out.println(pq);
    }
}
class MyComparator implements Comparator{
    public int compare(Object obj1Object obj2){
        String s1 = (String)obj1;
        String s2 = obj2.toString();
        return s2.compareTo(s1);
    }
}

Output:
[siva, kalyan, hemanth, chanukya]