Queue 1.5 version


  • This interface has introduced in 1.5 version.
  • If we want to represent, group of Objects Prior to processing, then we should go for Queue.
  • In general Queue follow FIFO, but we can customize based on our requirement.


  • From 1.5 version on wards linkedList also implement Queue interface.
  • LinkedList implementation of Queue always follows FirstInFirstOut.

Methods of Queue interface:-

  1. Boolean offer (object o )
    • For adding an object to the Queue.
  2. Object Peek()
    • Return head element of the Queue . If the Queue is empty this method returns null.
  3. Object elements().
    • Returns head element of the Queue. If Queue is empty, this method raises RuntimeException saying NoSuchElementException.
  4. Object remove()
    • Remove & returns head element of the Queue. If the Queue is empty, this method returns null.
  5. Object remove()
    • Remove & returns head element of the Queue. If Queue is empty, this method raises NoSuchElementException.

Priority Queue

  • It represents a data structure to hold a group of individual Objects prior to processing based on same priority.
  • It can be default natural sorting order or customized sorting order specified by comparator object.
  • If we are depending on default natural sorting order then objects should be homogeneous & comparable, otherwise we will get ClassCastException.
  • If we are defining our own sorting order, by comparator, then the objects need not be Homogeneous comparable.
  • Insertion order is not preserve.
  • Duplicate objects are not allow.
  • null insertion is not possible, even as the first element also.


PriorityQueue q = new PrioryQueue()

  1. Creates an empty priorityQueue with default initial capacity 11 & sorting order in natural sorting order.
  2. PriorityQueue q = new PriorityQueue(int initialcapacity);
  3. PriorityQueue q = new PriorityQueue (int initialcapacity, Comparator c);
  4. PriorityQueue q = new PriorityQueue (SortedSet s). // some platforms may not provide the support for PriorityQueues properly.


package com.narayanatutorial.collections.queue;

import java.util.NoSuchElementException;
import java.util.PriorityQueue;

public class PriorityQueueDemo1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		PriorityQueue<Integer> q = new PriorityQueue<Integer>();
		System.out.println(q.peek());// null		
		try {
			System.out.println(q.element()); // NoSuchElementException	
		}catch(NoSuchElementException e) {
			System.out.println("No Such Element Exception.."+e);
		for (int i = 0; i <= 10; i++) {
		System.out.println("PriorityQueue:"+q);// 10,1,2,3,....10
		System.out.println("poll:"+q.poll()); // 0
		System.out.println("PriorityQueue:"+q); // [1,2,3,4.....10]



No Such Element Exception..java.util.NoSuchElementException
PriorityQueue:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
PriorityQueue:[1, 3, 2, 7, 4, 5, 6, 10, 8, 9]


package com.narayanatutorial.collections.queue;

import java.util.Comparator;

public class Mycomparator implements Comparator<Object> {

	public int compare(Object obj1, Object obj2) {
		// TODO Auto-generated method stub
		String s1 = (String) obj1;
		String s2 = (String) obj2;
		return s2.compareTo(s1);




package com.narayanatutorial.collections.queue;

import java.util.PriorityQueue;

public class PriorityQueueDemo2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 PriorityQueue<String> q =new PriorityQueue<String>(new Mycomparator());
       System.out.println("customize sorting order:"+q); // [ZLBA] 
       PriorityQueue<String> q1 =new PriorityQueue<String>(new Mycomparator().reversed());
     System.out.println("customize sorting order with reversed method of comparator:"+q1); // [ZLBA] 



customize sorting order:[Z, L, B, A]
customize sorting order with reversed method of comparator:[A, B, Z, L]

Reference Links


Leave a Reply