Java Queue Interface

The Java.util package has the interface Queue, which extends the Collection interface. It is employed to preserve the components that are handled according to the FIFO principle. It is an ordered list of items where new elements are added at the end and old elements are removed from the beginning.

Being an interface, the queue needs a concrete class for the declaration, and the most popular classes in Java are the LinkedList and PriorityQueue. These classes’ implementations are not thread-safe. PriorityBlockingQueue is a viable solution if a thread-safe implementation is necessary.

Declaration :

public interface Queue<E> extends Collection<E> 

Methods of Java Queue Interface:

Method Description
boolean add(object) Inserts the specified element into the queue and returns true if successful.
boolean offer(object) Inserts the specified element into the queue.
Object remove() Used to retrieve and remove the queue’s head.
Object poll() return null if the queue is empty; else, it obtains and removes queue’s head.
Object element() It does retrieve, but does not remove, the queue’s head.
Object peek() returns null if the queue is empty, else it obtains the head of the queue without removing it.

Features of a Queue

  • A queue’s items are added to and removed using the FIFO paradigm.
  • All of the Collection interface’s methods, such as deletion, insertion, etc., are supported by the Java Queue.
  • LinkedList, ArrayBlockingQueue, and PriorityQueue are the most popular implementations of queue.
  • Any null operation on the blocking queues results in the NullPointerException being thrown.
  • Unbounded Queues are those Queues that are included in the util package.
  • Bounded Queues are those Queues that are included in the util.concurrent package.
  • All queues, with the exception of the Deques, make it easy to get in and out at the front and back of the line, respectively. Deques actually allow for element removal and insertion at both ends.

Implementation of Queue :

Java




import java.util.LinkedList;
import java.util.Queue;
 
public class QueueDemo {
 
    public static void main(String[] args)
    {
        Queue<Integer> q
            = new LinkedList<>();
 
        // Elements {10, 20, 30, 40, 50} are added to the queue
        for (int i = 10; i <= 50; i += 10)
            q.add(i);
 
        // Printing the contents of queue.
        System.out.println("The Elements of the queue are : "
                           + q);
 
        // Removing queue's head.
        int x = q.remove();
        System.out.println("Removed element - "
                           + x);
 
        System.out.println(q);
 
        // Viewing queue's head
        int head = q.peek();
        System.out.println("Head of the queue - "
                           + head);
 
        int size = q.size();
        System.out.println("Size of the queue - "
                           + size);
    }
}


Output

The Elements of the queue are : [10, 20, 30, 40, 50]
Removed element - 10
[20, 30, 40, 50]
Head of the queue - 20
Size of the queue - 4

Difference between PriorityQueue and Queue Implementation in Java

Similar Reads

Java Queue Interface

The Java.util package has the interface Queue, which extends the Collection interface. It is employed to preserve the components that are handled according to the FIFO principle. It is an ordered list of items where new elements are added at the end and old elements are removed from the beginning....

PriorityQueue Class

...

Difference between Queue and PriorityQueue Implementation :

Another class defined in the collection framework, PriorityQueue, provides a method for prioritising objects as they are processed. In the Java queue, object insertion and deletion are described as following a FIFO pattern. However, a PriorityQueue can be used when it is necessary to process queue elements in accordance with their priority....