English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map collection

Java Set collection

Java Input/Output (I/O)

Java Reader/Writer

Other Java topics

Java Queue Interface

In this tutorial, we will learn about the Java Queue interface and its methods.

The Queue interface of the Java collection framework provides the functions of queue data structure. It inherits the Collection interface.

Classes that implement the queue

Since Queue is an interface, we cannot provide its direct implementation.

To use the functions of Queue, we need to use the class that implements it:

Inherited Queue interface

The Queue interface can also be inherited by various sub-interfaces:

  • Deque

  • BlockingQueue

  • BlockingDeque

The workflow of queue data structure

In the queue, withFirst in first outto store and access elements. That is to say,Add elements from the back and delete elements from the front.

How to use Queue (Queue)?

In Java, you must import the java.util.Queue package to use Queue.

// Create using LinkedList
Queue<String> animal1 = new LinkedList<>();
// Created using ArrayDeque
Queue<String> animal2 = new ArrayDeque<>();
// Created using PriorityQueue
Queue<String> animal 3 = new PriorityQueue<>();

In this case, we have created objects of class LinkedList, ArrayDeque, and PriorityQueue for Animal1, Animal2and Animal3.These objects can use the functions of Queue interface.

Queue methods

Queue interface includes all methods of Collection interface. This is because Collection is the super interface of Queue.

Some commonly used methods of Queue interface:

  • add() - Insert the specified element into the queue. If the task is successful, add() returns true, otherwise throws an exception.

  • offer() - Insert the specified element into the queue. If the task is successful, offer() returns true, otherwise returns false.

  • element() - Return the head of the queue. If the queue is empty, throw an exception.

  • peek() - Return the head of the queue. If the queue is empty, return null.

  • remove() - Return and delete the head of the queue. If the queue is empty, throw an exception.

  • poll() - Return and delete the element at the beginning of the queue. If the queue is empty, return null.

Implementation of Queue interface

1.Implementation of LinkedList class

import java.util.Queue;
import java.util.LinkedList;
class Main {
    public static void main(String[] args) {
        // Create Queue using LinkedList class
        Queue<Integer> numbers = new LinkedList<>();
        //Add elements to Queue
        numbers.offer(1);
        numbers.offer(2);
        numbers.offer(3);
        System.out.println("Queue: " + numbers);
        // Access elements of Queue
        int accessedNumber = numbers.peek();
        System.out.println("Access Element: "); + accessedNumber);
        //Access elements from the Queue
        int removedNumber = numbers.poll();
        System.out.println("Remove Element: "); + removedNumber);
        System.out.println("Updated Queue: "); + numbers);
    }
}

Output Result

Queue:1, 2, 3]
Access elements: 1
Remove Element: 1
Updated Queue:2, 3]

For more information, please visitJava LinkedList.

2.Implementation of PriorityQueue class

import java.util.Queue;
import java.util.PriorityQueue;
class Main {
    public static void main(String[] args) {
        // Create a queue using the PriorityQueue class 
        Queue<Integer> numbers = new PriorityQueue<>();
        //Add elements to Queue
        numbers.offer(5);
        numbers.offer(1);
        numbers.offer(2);
        System.out.println("Queue: " + numbers);
        //Access Element of Queue
        int accessedNumber = numbers.peek();
        System.out.println("Access Element: "); + accessedNumber);
        //Remove Element from Queue
        int removedNumber = numbers.poll();
        System.out.println("Remove Element: "); + removedNumber);
        System.out.println("Updated Queue: "); + numbers);
    }
}

Output Result

Queue:1, 5, 2]
Access Element: 1
Remove Element: 1
Updated Queue:2, 5]

For more information, please visitJava PriorityQueue.

In the following tutorial, we will learn in detail about the different sub-interfaces and their implementations of the Queue interface.