A queue is a kind of linear data structure that is used to store elements in a particular manner. It stores the same type of elements. The elements in a queue are stored in a FIFO (First In, First Out) order. There are two ends in the queue i.e., front and rear.
The following figure describes the FIFO (First In, First Out) property of the Java queue.

We observe that the queue has two terminals, i.e., start (front) and end (rear). Elements are added inside the queue from the rear end and removed from the front end.
The Queue is an interface in the Java that belongs to Java.util package. It also extends the Collection interface.
The generic representation of the Java Queue interface is as follows:
public interface Queue<E> extends Collection<E>
Where, E is the type of elements held in this collection.
Note that we cannot create an instance of the interfaces. If we want to implement the functionality of the Queue interface, it is mandatory to have some solid classes that implement the Queue interface.
In Java, there are the following two classes that are used to implement the Queue interface.

The significant properties of the Java Queue data structure are given as follows:
Java provides the following classes to implement the Queue:
The Java interfaces are also used in the implementation of the Java queue. The interfaces that are used to implement the functionalities of the queue are given as follows:


In the Java queue, there are many methods that are used very commonly. The Queue interface promotes different methods like insert, delete, peek, etc. Some of the operations of the Java queue raise an exception whereas some of these operations return a particular value when the program is completed.
| Method | Description |
|---|---|
| boolean add(E e) | Adds element e to the queue at the end (tail) of the queue without violating the restrictions on the capacity. Returns true if success or IllegalStateException if the capacity is exhausted. |
| E peek() | Returns the head (front) of the queue without removing it. |
| E element() | Performs the same operation as the peek () method. Throws NoSuchElementException when the queue is empty. |
| E remove() | Removes the head of the queue and returns it. Throws NoSuchElementException if queue is empty. |
| E poll() | Removes the head of the queue and returns it. If the queue is empty, it returns null. |
| boolean offer(E e) | Insert the new element e into the queue without violating capacity restrictions. |
| int size() | Returns the size or number of elements in the queue. |
There are the following two ways to implement queue in Java:
Queue implementation is not as straightforward as a stack implementation.
To implement queue using Arrays, we first declare an array that holds n number of elements.
Then we define the following operations to be performed in this queue.
1) Enqueue: An operation to insert an element in the queue is Enqueue (function queue Enqueue in the program). For inserting an element at the rear end, we need first to check if the queue is full. If it is full, then we cannot insert the element. If rear < n, then we insert the element in the queue.
2) Dequeue: The operation to delete an element from the queue is Dequeue (function queue Dequeue in the program). First, we check whether the queue is empty. For dequeue operation to work, there has to be at least one element in the queue.
3) Front: It returns the front of the queue.
4) Display: It traverses the queue and displays the elements of the queue.
Java
class Queue {
private static int front, rear, capacity;
private static int queue[];
Queue(int size) {
front = rear = 0;
capacity = size;
queue = new int[capacity];
}
// insert an element into the queue
static void queueEnqueue(int item) {
// check if the queue is full
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
}
// insert element at the rear
else {
queue[rear] = item;
rear++;
}
return;
}
//remove an element from the queue
static void queueDequeue() {
// check if queue is empty
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}
// shift elements to the right by one place uptil rear
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
// set queue[rear] to 0
if (rear < capacity)
queue[rear] = 0;
// decrement rear
rear--;
}
return;
}
// print queue elements
static void queueDisplay() {
int i;
if (front == rear) {
System.out.printf("Queue is Empty\n");
return;
}
// traverse front to rear and print elements
for (i = front; i < rear; i++) {
System.out.printf(" %d, ", queue[i]);
}
return;
}
// print front of queue
static void queueFront() {
if (front == rear) {
System.out.printf("Queue is Empty\n");
return;
}
System.out.printf("\nFront Element of the queue: %d", queue[front]);
return;
}
}
public class Main {
public static void main(String[] args) {
// Create a queue of capacity 4
Queue q = new Queue(4);
System.out.println("Initial Queue:");
// print Queue elements
q.queueDisplay();
// inserting elements in the queue
q.queueEnqueue(10);
q.queueEnqueue(30);
q.queueEnqueue(50);
q.queueEnqueue(70);
// print Queue elements
System.out.println("Queue after Enqueue Operation:");
q.queueDisplay();
// print front of the queue
q.queueFront();
// insert element in the queue
q.queueEnqueue(90);
// print Queue elements
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf("\nQueue after two dequeue operations:");
// print Queue elements
q.queueDisplay();
// print front of the queue
q.queueFront();
}
}
Output:
Initial Queue: Queue is Empty Queue after Enqueue Operation: 10, 30, 50, 70, Front Element of the queue: 10 Queue is full 10, 30, 50, 70, Queue after two dequeue operations: 50, 70, Front Element of the queue: 50
In this program we will implement queue using LinkedList and perform the enqueue(), dequeue(), front(), and display() operations.
Java
class LinkedListQueue {
private Node front, rear;
private int queueSize; // queue size
//linked list node
private class Node {
int data;
Node next;
}
//default constructor - initially front & rear are null; size=0; queue is empty
public LinkedListQueue() {
front = null;
rear = null;
queueSize = 0;
}
//check if the queue is empty
public boolean isEmpty() {
return (queueSize == 0);
}
//Remove item from the front of the queue.
public int dequeue() {
int data = front.data;
front = front.next;
if (isEmpty()) {
rear = null;
}
queueSize--;
System.out.println("Element " + data+ " removed from the queue");
return data;
}
//Add data at the rear of the queue.
public void enqueue(int data) {
Node oldRear = rear;
rear = new Node();
rear.data = data;
rear.next = null;
if (isEmpty()) {
front = rear;
}
else {
oldRear.next = rear;
}
queueSize++;
System.out.println("Element " + data+ " added to the queue");
}
//print front and rear of the queue
public void print_frontRear() {
System.out.println("Front of the queue: " + front.data + " \nRear of the queue: " + rear.data);
}
}
public class Main {
public static void main(String a[]){
LinkedListQueue queue = new LinkedListQueue();
queue.enqueue(6);
queue.enqueue(3);
queue.print_frontRear();
queue.enqueue(12);
queue.enqueue(24);
queue.dequeue();
queue.dequeue();
queue.enqueue(9);
queue.print_frontRear();
}
}
Output:
Element 6 added to the queue Element 3 added to the queue Front of the queue: 6 Rear of the queue: 3 Element 12 added to the queue Element 24 added to the queue Element 6 removed from the queue Element 3 removed from the queue Element 9 added to the queue Front of the queue: 12 Rear of the queue: 9
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India