Java Queue Interface - Understanding FIFO Collection in Java
Explore the Java Queue Interface, a FIFO (First In First Out) collection structure, essential for managing elements in sequential processing. Learn how queues work within the java.util package, their use cases, and best practices for implementing queue operations efficiently in Java applications.
Java - Queue Interface
Queue Interface Overview
The Queue interface is part of the java.util
package and implements the Collection interface. It follows the FIFO (First In First Out) principle, meaning that the elements added first are the ones to be removed first. Queues are typically used to hold elements before processing them. Once an element is processed, it is removed from the queue, and the next item is selected for processing.
Queue Interface Declaration
public interface Queue<E> extends Collection<E>
Queue Interface Methods
The following is a list of important queue methods that all implementation classes of the Queue interface must implement:
Sr.No. | Method & Description |
---|---|
1 | boolean add(E e) : Inserts the specified element into this queue if possible without violating capacity restrictions, returning true upon success. Throws an IllegalStateException if no space is available. |
2 | E element() : Retrieves, but does not remove, the head of this queue. |
3 | boolean offer(E e) : Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions. |
4 | E peek() : Retrieves, but does not remove, the head of this queue, or returns null if the queue is empty. |
5 | E poll() : Retrieves and removes the head of this queue, or returns null if the queue is empty. |
6 | E remove() : Retrieves and removes the head of this queue. |
Methods Inherited
This interface inherits methods from the following interfaces:
java.util.Collection
java.lang.Iterable
Classes that Implement Queue
The following classes implement the Queue interface, allowing you to utilize queue functionalities:
LinkedList
ArrayDeque
PriorityQueue
Interfaces that Extend Queue
The following interfaces extend the Queue interface:
Deque
BlockingQueue
BlockingDeque
Example of Queue Interface
In this example, we demonstrate the usage of a LinkedList instance to perform queue operations such as add, peek, and size.
Syntax
package com.tutorialsarena;
import java.util.LinkedList;
import java.util.Queue;
public class QueueDemo {
public static void main(String[] args) {
Queue<Integer> q = new LinkedList<>();
q.add(10);
q.add(20);
q.add(30);
q.add(40);
q.add(50);
System.out.println("The queue is: " + q);
int num1 = q.remove();
System.out.println("The element deleted from the head is: " + num1);
System.out.println("The queue after deletion is: " + q);
int head = q.peek();
System.out.println("The head of the queue is: " + head);
int size = q.size();
System.out.println("The size of the queue is: " + size);
}
}
Output
The queue is: [10, 20, 30, 40, 50]
The element deleted from the head is: 10
The queue after deletion is: [20, 30, 40, 50]
The head of the queue is: 20
The size of the queue is: 4