Queue interface



Queue is another interface which implements Collection interface. This follows First In First Out (FIFO) manner in implementation. This interface has some special methods that can be used to work with it.

Implementing a queue

A queue can be implemented as follows.
  1. Using a LinkedList (java.util.LinkedList)
  2. Using a ProrityQueue (java.util.ProrityQueue)
  3. Using Java concurrency utilities (java.util.concurrent)

Special thing is, a queue which is implemented using Java concurrent package are bounded, others are unbounded. LinkedList is a better approach to implement a queue. Look at the following code to have an idea about queue.

    Queue priorityQueue = new PriorityQueue();
    Queue linkedListQueue = new LinkedList();


If you use a LinkedList to implement a queue, then specially you can add null values also. But it your are using a PriorityQueue, you cannot add null values.

There are some special methods that can be used with a queue. Look at the following methods and at the bottom you can see an example using these methods.

add() vs offer()

  • Both methods can be used to add elements to the queue.
  • But in PriorityQueue, you cannot add null values. It will throw NulPointerException. 
  • But you can add null values in LinkedList implementation of queue. 
  • Main difference can be seen when it happens any error while adding(Capacity exceeding).
  • At the time add() method throws IllegalStateException but offer() method returns false

    Queue<Integer> priorityQueue = new PriorityQueue<>();
    Queue<Integer> linkedListQueue = new LinkedList<>();
  
//  priorityQueue.add(null);
//  priorityQueue.offer(null);
    priorityQueue.add(10);
    priorityQueue.add(20);
    priorityQueue.offer(100);
    priorityQueue.offer(200);  
    System.out.println(priorityQueue);
  
    linkedListQueue.add(22);
    linkedListQueue.add(77);
    linkedListQueue.offer(33);
    linkedListQueue.add(null);
    linkedListQueue.offer(null);
    System.out.println(linkedListQueue);


remove() vs poll()

  • Both methods are used to return the head and remove it.
  • If queue is empty, remove() method throws NoSuchElementException but poll() method returns null.

    priorityQueue.remove();
    linkedListQueue.remove();
  
    priorityQueue.poll();
    linkedListQueue.poll();


element() vs peek()

  • Both methods are used to return the head of the queue.
  • If queue is empty element() methods throws NoSuchElementException but peek() method returns null.

    priorityQueue.element();
    linkedListQueue.element();
  
    priorityQueue.peek();
    linkedListQueue.peek();



Look at the following code which is implemented using a PriorityQueue for a complete reference.

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

   public class QueueDemo {

       public static void main(String[] args) {
  
           Queue<Integer> priorityQueue = new PriorityQueue<>();
  
           priorityQueue.add(10);
           priorityQueue.add(20);
           priorityQueue.offer(100);
           priorityQueue.offer(200);  
           System.out.println(priorityQueue);

           System.out.println("Remove head using remove()  :" + priorityQueue.remove());
           System.out.println(priorityQueue);
           System.out.println("Remove head using poll()    : " + priorityQueue.poll());
           System.out.println(priorityQueue); 

           System.out.println("Return head using element() : " + priorityQueue.element());
           System.out.println("Return head using peek()    : " + priorityQueue.peek());
  
           priorityQueue.removeAll(priorityQueue);
           System.out.println(priorityQueue);
  
           System.out.println(priorityQueue.poll());
           System.out.println(priorityQueue.peek());
//         System.out.println(priorityQueue.remove());
//         System.out.println(priorityQueue.element());
  
     }
}




Queue interface Queue interface Reviewed by Ravi Yasas on 4:40 PM Rating: 5

No comments:

Powered by Blogger.