Skip to main content

Java Queue Interface

Introduction to Java Queue Interface

The Queue interface in Java is part of the collections framework and is designed to hold elements that are about to be processed. It follows the FIFO (First In First Out) ordering i.e., the element that is added first is removed first. It can also be defined as 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 list (i.e., it follows the FIFO or the First-In-First-Out principle).

Structure of Queue Interface

The Queue interface in Java is a subtype of the java.util.Collection interface and it provides additional insertion, extraction, and inspection operations. The structure of the Queue Interface is given as follows:

public interface Queue<E> extends Collection<E>

This interface is parameterized, meaning you can define the type of elements it can contain.

Methods of Queue Interface

The Queue interface provides several methods to manipulate and retrieve elements from the queue. Here are some of the most used methods:

  • boolean add(E e): Adds the specified element into the queue and returns true if the queue was changed after the operation, throws an exception otherwise.
  • E remove(): Removes the head of the queue and returns it. Throws an exception if the queue is empty.
  • E element(): Returns the head of the queue but does not remove it. Throws an exception if the queue is empty.
  • boolean offer(E e): Adds the specified element into the queue and returns true if the queue was changed after the operation. Returns false otherwise.
  • E poll(): Removes the head of the queue and returns it. Returns null if the queue is empty.
  • E peek(): Returns the head of the queue but does not remove it. Returns null if the queue is empty.

Implementations of Queue Interface

The Queue interface has the following implementations in the Java Collections Framework:

  • LinkedList
  • PriorityQueue
  • ArrayBlockingQueue
  • ConcurrentLinkedQueue
  • DelayQueue
  • LinkedBlockingQueue
  • PriorityBlockingQueue
  • SynchronousQueue

Example of Queue Interface

Here is an example of how to use the Queue interface in Java:

import java.util.*;

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. Output would be [0, 1, 2, 3, 4]
System.out.println("Elements of queue: "+ q);

// Removing the head of queue. Output would be 0
int removedHead = q.remove();
System.out.println("removed element: " + removedHead);

// Display contents of the queue. Output would be [1, 2, 3, 4]
System.out.println("Elements of queue after remove: "+ q);

// Checking the head of queue. Output would be 1
int head = q.peek();
System.out.println("Head of queue: " + head);
}
}

In this example, a queue of integers is created and elements are added to it. The head of the queue is removed and checked, and the elements of the queue are displayed after each operation.

Conclusion

The Queue interface in Java is a powerful tool for managing elements in a FIFO manner. It provides methods for inserting, removing, and inspecting elements. The interface is implemented by several classes in the Java Collections Framework, providing different types of queues depending on your needs.