Queue Interface in Java

A Queue is designed in such a way so that the elements added to it are placed at the end of Queue and removed from the beginning of Queue. The Queue interface is based on the basic principle of last-in-first-out.

Introduction

 
The java.util.queue is an interface in Java and extends from java.util.Collection. In this article, we will learn about the Queue interface, its methods, and the types of Queue provided by the Java programming language.
 

Queue Interface in Java

 
A Queue is a FIFO (First In First Out) abstract data type (ADT). In other words, the elements are removed in the order in which they were inserted. The java.util.queue is an interface in Java and extends from java.util.Collection. Some of the commonly used Queue implementation classes include a LinkedList, an ArrayDeque and a PriorityQueue.
 
The Queue collection is used to hold the elements about to be processed and provides various operations like the insertion, removal, etc. It is an ordered list of objects with its use limited to insert elements at the end of the list and deleting elements from the start of the list i.e. it follows the FIFO or the First-In-First-Out principle. Being an interface the queue needs a concrete class for the declaration and the most common classes are the PriorityQueue and LinkedList in Java. It is to be noted that both the implementations are not thread-safe.
 
java-queue-interface
 
In the queue, the first element is inserted from one end called the REAR(also called tail).
 
In the queue, the removal of an existing element takes place from the other end called FRONT(also called head).
 

Operations in Queue interface in Java

 
In Java, two operations are performed by queue interface. For inseration Enqueue and for deletion Dequeue.
  • Enqueue: Enqueue adds an element at the beginning. If the queue is full, then it is overflow.
    Enqueue performs the following tasks in the queue for example:
    1) Checks if the queue is full.
    2) If the queue if full, then print"Queue overflow".
    3) Else increment REAR by 1.
    4) Assign QUEUE[REAR] = ELEMENT.
     
  • Dequeue: Dequeue deletes an element at the end of the queue. If the queue is empty, then it underflows.
    Dequeue performs the following tasks in the queue for example:
    1) Checks if the queue is empty.
    2) If the queue if full, then print"Queue "underflow.
    3) Copy the element at the front of the queue to some temporary variable, TEMP= QUEUE[FRONT].
    4) Increment FRONT by 1.
    5) print temp and delete it.
The elements in the queue are arranged sequentially and that's why the queue is said to be Linear data structure.
 
operations-java-queue-interface 
 
For example a line of the customers in a shop. So, the customer who will come the first to the shop will be the who will get the service first and so on.
 
The complete program of the queue interface example in Java is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample1 {  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         q.add(6);  
  7.         q.add(1);  
  8.         q.add(8);  
  9.         q.add(4);  
  10.         q.add(7);  
  11.         System.out.println("The queue is: " + q);  
  12.     }  
The above program generates the following output.
 
queue-example1-output 
 

Methods of Queue interface in Java

 
The java.lang.Math class contains various methods for performing basic numeric operations such as the logarithm, cube root, and trigonometric functions, etc. The various java math methods are as follows:
 

1) queue.add() method in Java

 
This method adds the specified element at the end of the Queue. Returns true if the element is added successfully or false if the element is not added that happens when the Queue is at its max capacity and cannot take any more elements.
 
The complete program of queue.add() method example is listed below.
  1. import java.util.LinkedList;   
  2. import java.util.Queue;   
  3. public class QueueExample   
  4. {   
  5.   public static void main(String[] args)   
  6.   {   
  7.     Queue<Integer> q = new LinkedList<>();   
  8.     
  9.     // Adds elements {0, 1, 2, 3, 4} to queue   
  10.     for (int i=0; i<5; i++)   
  11.      q.add(i);   
  12.     
  13.     // Display contents of the queue.   
  14.     System.out.println("Elements of queue-"+q);   
  15.   }  
  16. }  
The above program generates the following output.
 
queue-example2-output 
 

2) queue.offer() method in Java 

 
The queue.offer() method of Queue Interface inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. This method takes an e parameter which is the element that has to be added into the stack. It returns true if the element is inserts otherwise returns false.
 
The complete program of the queue.offer() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample3 {  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.   
  7.         // Adds elements {0, 1, 2, 3, 4} to queue  
  8.         for (int i = 0; i < 5; i++)  
  9.             q.add(i);  
  10.         System.out.println("Elements of queue-" + q);  
  11.         if (q.offer(10)) {  
  12.             System.out.println("if the queue is not full 10 is added into the queue");  
  13.             System.out.println("Elements of queue-" + q);  
  14.         } else System.out.println("Queue is full");  
  15.     }  
The above program generates the following output.
 
queue-example3-output 
 

3) queue.peek() method in Java

 
The queue.peek() method returns the element at the head of the queue. It returns null if the queue is empty. The element is not removed from the queue.
 
The complete program of queue.peek() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3.   
  4. public class QueueExample4 {  
  5.     public static void main(String[] args) {  
  6.         Queue<Integer> q = new LinkedList<>();  
  7.         // Adds elements {0, 1, 2, 3, 4} to queue  
  8.         for (int i = 0; i < 5; i++)  
  9.             q.add(i);  
  10.         System.out.println("Elements of queue-" + q);  
  11.         System.out.println("The head of the queue is : " + q.peek());  
  12.     }  
The above program generates the following output.
 
queue-example4-output 
 
Another example of showing the result of queue.peek() method if the queue is empty.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample5 {  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         System.out.println("The head of the queue is : " + q.peek());  
  7.     }  
The above program generates the following output.
 
queue-example5-output 
 

4) queue.poll() method in Java

 
The queue.poll() method retrieves and removes the head of this queue, or returns null if this queue is empty. The complete program of queue.poll() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample6 {  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         q.add(6);  
  7.         q.add(1);  
  8.         q.add(8);  
  9.         q.add(4);  
  10.         q.add(7);  
  11.         System.out.println("The queue is: " + q);  
  12.         System.out.println("The head of the queue is : " + q.poll());  
  13.     }  
The above program generates the following output.
 
queue-example6-output 
 

5) queue.size() method in Java 

 
The queue.size() method used to get the number of elements in the Set. The complete program of queue.size() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample7 {  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         q.add(6);  
  7.         q.add(1);  
  8.         q.add(8);  
  9.         q.add(4);  
  10.         q.add(7);  
  11.         System.out.println("The queue is: " + q);  
  12.         System.out.println("The head of the queue is : " + q.size());  
  13.     }  
The above program generates the following output.
 
queue-example7-output 
 

6) queue.isEmpty() method in Java

 
The queue.empty() method is used to check the queue is empty or not. It returns true if the queue is empty otherwise false. The complete program of queue.empty() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample8{  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         q.add(6);  
  7.         q.add(1);  
  8.         q.add(8);  
  9.         q.add(4);  
  10.         q.add(7);  
  11.         System.out.println("The queue is: " + q);  
  12.         System.out.println("The queue id empty ? " + q.isEmpty());  
  13.     }  
The above program generates the following output.
 
queue-example8-output 
 

7) queue.contains() method in Queue

 
The queue.contains() returns true if this set contains the specified element. It takes an argument as the specified element which has to be searched into the queue. The complete program of the queue.contains() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample9{  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         q.add(6);  
  7.         q.add(1);  
  8.         q.add(8);  
  9.         q.add(4);  
  10.         q.add(7);  
  11.         System.out.println("The queue is: " + q);  
  12.         if(q.contains(12)){  
  13.             System.out.println("The queue has element 12 ");  
  14.   
  15.         } else  
  16.         System.out.println("The queue does not have 12 ? ");  
  17.     }  
The above program generates the following output.
 
queue-example9-output 
 

8) queue.remove() method in Queue

 
The queue.remove() method retrieves and removes the head of this queue. The complete program of queue.remove() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample11{  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         q.add(6);  
  7.         q.add(1);  
  8.         q.add(8);  
  9.         q.add(4);  
  10.         q.add(7);  
  11.         System.out.println("The queue is: " + q);  
  12.         System.out.println("removed element from the queue is : " +q.remove());  
  13.     }  
The above program generates the following output.
 
queue-example11-output
 

9) queue.removeAll() method in Queue

 
The queue.removeAll() method removes from this set all of its elements that are contained in the specified collection (optional operation). It takes the collection as the argument and returns true if the elements are removed otherwise false.
 
The complete program of the queue.removeAll() method example is listed below.
  1. import java.util.LinkedList;  
  2. import java.util.Queue;  
  3. public class QueueExample12{  
  4.     public static void main(String[] args) {  
  5.         Queue<Integer> q = new LinkedList<>();  
  6.         q.add(6);  
  7.         q.add(1);  
  8.         q.add(8);  
  9.         q.add(4);  
  10.         q.add(7);  
  11.         System.out.println("The queue is: " + q);  
  12.         System.out.println("All elements from the queue is removed : "+ q.removeAll(q));  
  13.     }  
The above program generates the following output.s
 
queue-example12-output 
 

Summary

 
In this article, we learned about the Queue interface and its methods in Java programming language and how to use them into Java programs.