Queue Interface in Java

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 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 the queue interface, for insertion 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. Check if the queue is full.
  2. If the queue is full, then print"Queue overflow".
  3. Else increment REAR by 1.
  4. 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. Check if the queue is empty.
  2. If the queue is 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 a Linear data structure.

operations-java-queue-interface 

For example, a line of the customers in a shop. So, the customer who will come first to the shop will be the one who will get the service first, and so on.

The complete program of the queue interface example in Java is listed below.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample1 {  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        q.add(6);  
        q.add(1);  
        q.add(8);  
        q.add(4);  
        q.add(7);  
        System.out.println("The queue is: " + q);  
    }  
} 

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.

import java.util.LinkedList;   
import java.util.Queue;   
public class QueueExample   
{   
  public static void main(String[] args)   
  {   
    Queue<Integer> q = new LinkedList<>();   
    
    // Adds elements {0, 1, 2, 3, 4} to queue   
    for (int i=0; i<5; i++)   
     q.add(i);   
    
    // Display contents of the queue.   
    System.out.println("Elements of queue-"+q);   
  }  
}  

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.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample3 {  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
  
        // Adds elements {0, 1, 2, 3, 4} to queue  
        for (int i = 0; i < 5; i++)  
            q.add(i);  
        System.out.println("Elements of queue-" + q);  
        if (q.offer(10)) {  
            System.out.println("if the queue is not full 10 is added into the queue");  
            System.out.println("Elements of queue-" + q);  
        } else System.out.println("Queue is full");  
    }  
} 

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.

import java.util.LinkedList;  
import java.util.Queue;  
  
public class QueueExample4 {  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        // Adds elements {0, 1, 2, 3, 4} to queue  
        for (int i = 0; i < 5; i++)  
            q.add(i);  
        System.out.println("Elements of queue-" + q);  
        System.out.println("The head of the queue is : " + q.peek());  
    }  
} 

The above program generates the following output.

queue-example4-output 

Another example of showing the result of a queue.peek() method if the queue is empty.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample5 {  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        System.out.println("The head of the queue is : " + q.peek());  
    }  
} 

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.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample6 {  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        q.add(6);  
        q.add(1);  
        q.add(8);  
        q.add(4);  
        q.add(7);  
        System.out.println("The queue is: " + q);  
        System.out.println("The head of the queue is : " + q.poll());  
    }  
} 

The above program generates the following output.

queue-example6-output 

5) queue.size() method in Java 

The queue.size() method is used to get the number of elements in the Set. The complete program of queue.size() method example is listed below.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample7 {  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        q.add(6);  
        q.add(1);  
        q.add(8);  
        q.add(4);  
        q.add(7);  
        System.out.println("The queue is: " + q);  
        System.out.println("The head of the queue is : " + q.size());  
    }  
} 

The above program generates the following output.

queue-example7-output 

6) queue.isEmpty() method in Java

The queue.empty() method is used to check whether 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.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample8{  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        q.add(6);  
        q.add(1);  
        q.add(8);  
        q.add(4);  
        q.add(7);  
        System.out.println("The queue is: " + q);  
        System.out.println("The queue id empty ? " + q.isEmpty());  
    }  
} 

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.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample9{  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        q.add(6);  
        q.add(1);  
        q.add(8);  
        q.add(4);  
        q.add(7);  
        System.out.println("The queue is: " + q);  
        if(q.contains(12)){  
            System.out.println("The queue has element 12 ");  
  
        } else  
        System.out.println("The queue does not have 12 ? ");  
    }  
} 

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.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample11{  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        q.add(6);  
        q.add(1);  
        q.add(8);  
        q.add(4);  
        q.add(7);  
        System.out.println("The queue is: " + q);  
        System.out.println("removed element from the queue is : " +q.remove());  
    }  
} 

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.

import java.util.LinkedList;  
import java.util.Queue;  
public class QueueExample12{  
    public static void main(String[] args) {  
        Queue<Integer> q = new LinkedList<>();  
        q.add(6);  
        q.add(1);  
        q.add(8);  
        q.add(4);  
        q.add(7);  
        System.out.println("The queue is: " + q);  
        System.out.println("All elements from the queue is removed : "+ q.removeAll(q));  
    }  
} 

The above program generates the following output.

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 in Java programs.


Similar Articles