Queue Interface in Java

In this tutorial, we will learn about Queue Interface in Java and some of its implementation classes and their usage.

Queue Interface in Java is present in java.util package, which represents a queue of elements in which elements will be inserted and removed in a FIFO manner.

In other words, elements are inserted at the end of the queue and removed from the beginning of the queue (maintaining FIFO order)

The Queue interface in Java is a subtype of the Java Collection, which means it inherits properties of the collection framework as well.

How to Declare Queue in Java?

Let’s see how to create Queue in Java:

Queue<T> queueA = new PriorityQueue();

Here, “T” indicates the type of a Queue. For example,

// create Integer type Queue
Queue<Integer> queueA = new PriorityQueue();
Queue<Integer> queueB = new LinkedList();

Queue Interface in Java

Below is the exact code of the Queue Interface present in JDK. You can see what methods Queue Interface in Java provides.

package java.util;

public interface Queue<E> extends Collection<E> {
    boolean add(E var1);

    boolean offer(E var1);

    E remove();

    E poll();

    E element();

    E peek();

Any of its implementation class will leverage these methods.

Java Queue Methods:

Method Description
add(E e)Inserts the element into the queue. Returns true if successful, if not it throws an exception.
remove()Retrieves and removes the element from the beginning
peek()Only retrieves the head of this queue, or returns null if this queue is empty.
poll()Retrieves and removes the head of the queue, or returns null if this queue is empty.
element()Only retrieves the head of this queue. or throws an exception if the queue is empty.
offer(E e)Inserts the specified element into the queue. Returns true if successful, if not return false

Java Queue Example:

Below is a sample program of Queue in Java, using PriorityQueue.

public class QueueExample {

    public static void main(String[] args) {

        Queue<Integer> queueA = new PriorityQueue();


        System.out.println("Printing Queue ---> " + queueA);

        System.out.println("Peek Element ---> " + queueA.peek());


Printing Queue ---> [1, 2, 3, 4, 5]
Peek Element ---> 1

Queue Implementation Classes:

You must be wondering why we have used PriorityQueue in the above example.

It’s simple. The Queue is an interface, therefore, you need to instantiate a concrete implementation of the interface in order to use it.

Below are some general-purpose implementation classes that you can use to instantiate a Queue.

  • java.util.PriorityQueue
  • java.util.LinkedList
  • java.util.ArrayDeque

1. PriorityQueue

It provides the functionality of the heap data structure. The elements of the priority queue are ordered according to their natural ordering. It does not permit null elements.

2. LinkedList

It is a part of the Java Collections Framework and implementation of the List and Deque Interface. It provides the functionality of doubly linkedlist data structure.

3. ArrayDeque

ArrayDeuqe is a resizable-array implementation of the Deque interface. ArrayDeque is not thread-safe and Null elements are not allowed.

TIP: ArrayDeque is faster than Stack when used as a Stack, and faster than LinkedList when used as a Queue.

That’s it for the post, I hope you understand What is Queue Interface in Java? and where you can use it. You can read further posts for a better understanding of its implementations.

For Official Docs: Read Here

Set Interface in Java

Deque Interface in Java

Leave a Comment