Queues Interface in Java

Queue Interface:

The Queue Interface in Java Collections provide functionality as a Queue in the real-life, it extends the collections interface. It represents real-world queues where people queue in waiting line in front of the Supermarket where new people add up at the end of the line (queue) and the people are served at the front of the line (queue) similarly ina java queue we add elements at the back of the queue and remove elements from the front of the queue. In a First In, First Out manner.

The adding of new elements are called enqueue and removing elements from the queue is called dequeue

Implementation of Queue:

Since the Queue is an interface, we cannot provide the direct implementation of it.

To use the functionalities of Queue, we need to use classes that implement it:

Queue Data Structure

In queues, elements are stored and accessed in First In, First Out manner. That is, elements are added from the behind and removed from the front.

Syntax:

Queue<type> varName = new LinkedList<>();

//Queue implementation using LinkedList.

Queue<type> varName = new ArrayDeque<>();

//Queue implementation using ArrayDeque.

Queue<type> varName = new PriorityQueue<>();

//Queue implementation using PriorityQueue.

Methods:

The Queue interface has all the methods of the collection interface as it extends the collection interface and all of its methods.

Commonly used methods are:

add() : Enters the specified element at the end of the queue. Returns true after successful insertion and IllegalStateException if false.

element() : Returns the head of the queue, but does not remove it and throws an exception if there is no element in the queue

offer() : This is of boolean type and it inserts the element into the queue.

poll() : Returns and removes the head of the queue and returns NULL when the queue is empty.

peek() : Returns but does not remove the head of the queue, returns NULL if empty.

 

Implementation of the Queue Interface

Implementing the LinkedList Class and PriorityQueue Class

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

class Main {

    public static void main(String[] args) {
        //Queue using the LinkedList class
        Queue<Integer> num1 = new LinkedList<>();
        //Queue using the PriorityQueue class
        Queue<Integer> num2= new PriorityQueue<>();

        // adding elements to the Queue
        num1.offer(8);
        num1.offer(4);
        num1.offer(2);
        System.out.println("Queue using Linked List: " + num1);

       // adding elements to the Queue
        num2.offer(5);
        num2.offer(10);
        num2.offer(15);
        System.out.println("Queue using Priority Queue: " + num2);

        // Access elements of the Queue
        int accessedNumber = num1.peek();
        System.out.println("Accessed Element from the LinkedList implementation: " + number1);
        int accessedNumber = num2.peek();
        System.out.println("Accessed Element from the PriorityQueue implementation: " + number2);


        // Remove elements from the Queue
        int head1 = num1.poll();
        System.out.println("Removed Element from the LinkedList implementation: " + removedNumber);
        int head2 = num2.poll();
        System.out.println("Removed Element from the PriorityQueue implementation: " + removedNumber);
        System.out.println("Updated Queue(Linked List Implementation)");
        System.out.println("Updated Queue1: " + num1);
        System.out.println("Updated Queue(Priority Queue Implementation)");
        System.out.println("Updated Queue2: " + num2);
    }
}

Output

Queue using Linked List: [2, 4, 8]
Queue using Priority Queue: [15, 10, 5]
Accessed Element from LinkedList Implementation: 2
Accessed Element from PriorityQueue Implementation: 15
Removed Element from the LinkedList Implementation: 2 
Removed Element from the PriorityQueue Implementation: 15 
Updated Queue(Linked List Implementation): [4, 8]
Updated Queue(Priority Queue Implementation): [10, 5]

Queues are simple and are very useful as they have many use in Operating System scheduling mechanisms