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 )
1 week ago - Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation).
🌐
GeeksforGeeks
geeksforgeeks.org › java › queue-interface-java
Queue Interface In Java - GeeksforGeeks
To add an element in a queue, we can use the add() method. The insertion order is not retained in the PriorityQueue. The elements are stored based on the priority order which is ascending by default. Java ·
Published   5 days ago
🌐
Programiz
programiz.com › java-programming › queue
Java Queue Interface
In queues, elements are stored and accessed in First In, First Out manner. That is, elements are added from the behind and removed from the front. In Java, we must import java.util.Queue package in order to use Queue.
🌐
IIT Kanpur
iitk.ac.in › esc101 › 05Aug › tutorial › collections › interfaces › queue.html
The Queue Interface
The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the java.util.concurrent.BlockingQueue interface, which extends Queue.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-queue
Java Queue – Queue in Java | DigitalOcean
August 4, 2022 - Java Queue follows FIFO order to insert and remove it’s elements. FIFO stands for First In First Out. Java Queue supports all methods of Collection interface.
🌐
Baeldung
baeldung.com › home › java › java collections › guide to the java queue interface
Guide to the Java Queue Interface | Baeldung
January 8, 2024 - When we create a custom queue extending the AbstractQueue class, we must provide an implementation of the offer method which does not allow the insertion of null elements. Additionally, we must provide the methods peek, poll, size, and java.util‘s iterator.
🌐
CodeGym
codegym.cc › java blog › java collections › java queue interface and implementations
Java Queue Interface and implementations
There are many classes that implement ... doesn’t allow null elements. There are 3 more methods add, remove, and element based on Queue classical offer, poll, and peek, respectively....
Published   January 16, 2025
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()
Find elsewhere
🌐
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 - For dequeue operation to work, there has to be at least one element in the queue. #3) Front: This method returns the front of the queue. #4) Display: This method traverses the queue and displays the elements of the queue. The following Java program demonstrates the Array implementation of Queue.
🌐
Simplilearn
simplilearn.com › home › resources › software development › an introduction to queue in java with example
An Introduction to Queue in Java with Example
June 9, 2025 - The java queue contains multiple elements before the process. The order of elements of the queue in java is FIFO, that is, first-in-first-out. Learn more now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Tutorialspoint
tutorialspoint.com › java › java_util_queue.htm
Java - Queue Interface
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); } }
🌐
Oracle
docs.oracle.com › javase › tutorial › collections › implementations › queue.html
Queue Implementations (The Java™ Tutorials > Collections > 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 queue based on the heap data structure.
🌐
Jenkov
jenkov.com › tutorials › java-collections › queue.html
Java Queue
April 20, 2020 - It represents an ordered sequence of objects just like a Java List, but its intended use is slightly different. Because the Java Queue interface is a subtype of the Java Collection interface, all methods in the Collection interface are also available in the Queue interface.
🌐
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 - This means that elements are added to the end of the queue and removed from the front. The `Queue` interface extends the `Collection` interface and provides additional methods for inserting, removing, and inspecting elements.
🌐
Oracle
docs.oracle.com › javase › tutorial › collections › interfaces › queue.html
The Queue Interface (The Java™ Tutorials > Collections > Interfaces)
The Queue interface does not define the blocking queue methods, which are common in concurrent programming. These methods, which wait for elements to appear or for space to become available, are defined in the interface java.util.concurrent.BlockingQueue, which extends Queue.
🌐
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().
🌐
HappyCoders.eu
happycoders.eu › algorithms › java-queue
Queue Interface in Java (+ Code Examples)
November 27, 2024 - The Queue interface defines six methods for inserting, removing, and viewing elements.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › queue in java
Queue in Java: Implementation, Methods, and Examples
April 28, 2025 - Queue in Java follows the First-In, First-Out (FIFO) principle. Elements are inserted at the rear and removed from the front. Java provides the Queue interface and classes like LinkedList and PriorityQueue to efficiently manage the ordering ...
🌐
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 - Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations. Each of these methods exists in two forms: one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation).
🌐
Medium
medium.com › codex › queue-data-structure-implementation-in-java-d53bb991a890
Queue Data Structure Implementation in Java | by shivam bhatele | CodeX | Medium
April 11, 2023 - size()- This method is used to Return the size or number of elements which is present in the queue. Now that we know that a queue is an interface in java so let us see the syntax of how to implement a queue in java: Since the queue is an inbuilt method in java so firstly we have to write the following line to import a queue in java: