Use:

Queue<Object> queue = new LinkedList<>();

You can use .offer(E e) to append an element to the end of the queue and .poll() to dequeue and retrieve the head (first element) of the queue.

Java defined the interface Queue, the LinkedList provided an implementation.

It also maintains references to the Head and Tail elements, which you can get by .getFirst() and .getLast() respectively.


credit to @Snicolas for suggesting queue interface

Answer from didxga on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Queue.html
Queue (Java Platform SE 8 )
2 weeks ago - Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. ... ClassCastException - if the class of the specified element prevents it from being added to this queue
🌐
GeeksforGeeks
geeksforgeeks.org › java › queue-interface-java
Queue Interface In Java - GeeksforGeeks
Elements follow FIFO ... a Queue directly as it is an interface. Instead, we use classes like PriorityQueue, LinkedList, or ArrayDeque that implement the Queue interface....
Published   2 weeks ago
Top answer
1 of 9
108

Use:

Queue<Object> queue = new LinkedList<>();

You can use .offer(E e) to append an element to the end of the queue and .poll() to dequeue and retrieve the head (first element) of the queue.

Java defined the interface Queue, the LinkedList provided an implementation.

It also maintains references to the Head and Tail elements, which you can get by .getFirst() and .getLast() respectively.


credit to @Snicolas for suggesting queue interface

2 of 9
55

If you use LinkedList be careful. If you use it like this:

LinkedList<String> queue = new LinkedList<String>();

then you can violate queue definition, because it is possible to remove other elements than first (there are such methods in LinkedList).

But if you use it like this:

Queue<String> queue = new LinkedList<String>();

it should be ok,as this is heads-up to users that insertions should occur only at the back and deletions only at the front.

You can overcome defective implementation of the Queue interface by extending the LinkedList class to a PureQueue class that throws UnsupportedOperationException of any of the offending methods. Or you can take approach with aggreagation by creating PureQueue with only one field which is type LinkedList object, list, and the only methods will be a default constructor, a copy constructor, isEmpty(), size(), add(E element), remove(), and element(). All those methods should be one-liners, as for example:

/**
* Retrieves and removes the head of this queue.
* The worstTime(n) is constant and averageTime(n) is constant.
*
* @return the head of this queue.
* @throws NoSuchElementException if this queue is empty.
*/
public E remove()
{
    return list.removeFirst();
} // method remove()
🌐
Princeton University
algs4.cs.princeton.edu › 13stacks › Queue.java.html
Queue.java
February 8, 2023 - * * @author Robert Sedgewick * @author Kevin Wayne * * @param <Item> the generic type of each item in this queue */ public class Queue<Item> implements Iterable<Item> { private Node<Item> first; // beginning of queue private Node<Item> last; // end of queue private int n; // number of elements on queue // helper linked list class private static class Node<Item> { private Item item; private Node<Item> next; } /** * Initializes an empty queue.
🌐
Baeldung
baeldung.com › home › java › java collections › guide to the java queue interface
Guide to the Java Queue Interface | Baeldung
January 8, 2024 - Generally, the Queue interface is inherited by 3 main sub-interfaces. Blocking Queues, Transfer Queues, and Deques. Together, these 3 interfaces are implemented by the vast majority of Java’s available Queues.
🌐
CodeGym
codegym.cc › java blog › java collections › java queue interface and implementations
Java Queue Interface and implementations
According to Oracle documentation, Queue interface has 2 superinterfaces, 4 different interfaces that inherit from the queue, and an extremely impressive list of classes. What does it mean? First of all, Java Queue is a part of the Collection Framework and implements Collection interface.
Published   January 16, 2025
🌐
Programiz
programiz.com › java-programming › queue
Java Queue Interface
In Java, we must import java.util.Queue package in order to use Queue. // LinkedList implementation of Queue Queue<String> animal1 = new LinkedList<>(); // Array implementation of Queue Queue<String> animal2 = new ArrayDeque<>(); // Priority Queue implementation of Queue Queue<String> animal3 = new PriorityQueue<>(); Here, we have created objects animal1, animal2 and animal3 of classes LinkedList, ArrayDeque and PriorityQueue respectively.
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-queue
Java Queue – Queue in Java | DigitalOcean
August 4, 2022 - Java Queue represents an ordered list of elements. Java Queue follows FIFO order to insert and remove it’s elements.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Queue.html
Queue (Java Platform SE 7 )
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available. ... ClassCastException - if the class of the specified element prevents it from being added to this queue
🌐
Medium
medium.com › @pratik.941 › mastering-the-queue-interface-in-java-6e4710036eee
Mastering the Queue Interface in Java | by Pratik T | Medium
June 3, 2024 - Mastering the Queue Interface in Java The `Queue` interface is a part of the Java Collections Framework and represents a collection designed for holding elements prior to processing. Queues are an …
🌐
Princeton CS
introcs.cs.princeton.edu › java › code › javadoc › Queue.html
Queue
The Queue class represents a first-in-first-out (FIFO) queue of generic items.
🌐
Oracle
docs.oracle.com › javase › tutorial › collections › implementations › queue.html
Queue Implementations (The Java™ Tutorials > Collections > Implementations)
See Java Language Changes for a ... in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. The Queue implementations are grouped into general-purpose and concurrent implementations. As mentioned in the previous section, LinkedList implements the Queue interface, providing first in, first out (FIFO) queue operations for add, poll, and so on. The PriorityQueue class is a priority ...
🌐
Tutorialspoint
tutorialspoint.com › java › java_util_queue.htm
Java - Queue Interface
Following is the list of the important queue methods that all the implementation classes of the Queue interface implement − · This interface inherits methods from the following interfaces − ... In this example, we're using a LinkedList instance to show queue add, peek and size operations. package com.tutorialspoint; import java.util.LinkedList; import java.util.Queue; public class QueueDemo { public static void main(String[] args) { Queue<Integer> q = new LinkedList<>(); q.add(6); q.add(1); q.add(8); q.add(4); q.add(7); 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); } }
🌐
Codecademy
codecademy.com › learn › linear-data-structures-java › modules › queues-java › cheatsheet
Linear Data Structures: Queues Cheatsheet | Codecademy
The .enqueue() method of the Java Queue class is used to add new data to the queue. It takes a single argument, data, which is added to the end of the queue using the LinkedList method .addToTail(). A print statement can be included to describe the addition. The method then increases size and throws an error if the queue is full.
🌐
HappyCoders.eu
happycoders.eu › algorithms › java-queue
Queue Interface in Java (+ Code Examples)
November 27, 2024 - Queue<Integer> queue = new ConcurrentLinkedQueue<>();Code language: Java (java) (I will explain the different queue classes in later parts of this tutorial.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › util › Queue.html
Queue (Java SE 11 & JDK 11 )
January 20, 2026 - Queue implementations generally do not define element-based versions of methods equals and hashCode but instead inherit the identity based versions from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties. This interface is a member of the Java Collections Framework.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › understanding java queue interface
Understanding Java Queue Interface
June 18, 2025 - The Queue interface is an abstraction. You cannot create an object directly from it. You must use a concrete class that implements Queue. Java provides several such classes.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java queue – queue methods, queue implementation with examples
Java Queue - Queue Methods, Queue Implementation & Example
April 1, 2025 - The Java queue interface provides all the methods of Collection interface like insertion, deletion, etc. LinkedList and PriorityQueue are the classes that implement the Queue interface.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › util › Queue.html
Queue (Java SE 17 & JDK 17)
January 20, 2026 - Queue implementations generally do not define element-based versions of methods equals and hashCode but instead inherit the identity based versions from class Object, because element-based equality is not always well-defined for queues with the same elements but different ordering properties. This interface is a member of the Java Collections Framework.