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 days 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   2 weeks 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.
🌐
Quora
quora.com › How-do-you-implement-a-queue-in-Java
How to implement a queue in Java - Quora
Answer: you can try something like this:- [code]import java.util.LinkedList; import java.util.Queue; class Test { public static void main(String[] args) { Queue queue = new LinkedList (); //Inserting elements in the Queue. queue.add("A"); queue...
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
🌐
CodeGym
codegym.cc › java blog › java collections › java queue interface and implementations
Java Queue Implementation and Interface
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
🌐
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.
🌐
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.
🌐
Medium
medium.com › @dowaj › java-the-queue-interface-and-priorityqueues-d6702a58ff02
Java: The Queue Interface And PriorityQueues. | by Doctor DoWell | Medium
June 23, 2024 - In Java, The Queue Interface is present in java.util package and is typically implemented as a LinkedList of objects which are inserted at the end and retrieved from the start, respectively.
🌐
CalliCoder
callicoder.com › java-queue
Java Queue Interface Tutorial with Examples | CalliCoder
February 18, 2022 - Iterate over a Queue using iterator() and Java 8 forEachRemaining() method. Iterate over a Queue using simple for-each loop. The iteration order in a Queue is same as the insertion order.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java queue interface
Java Queue Interface | Methods and Examples of Java Queue Interface
June 21, 2023 - It is an ordered sequence of objects like a java list. ... Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more. In the following syntax, An object obj is instantiated using the LinkedList / PriorityQueue class. In the below two Queue syntax, LinkedList implementation is the standard one. ... Queue instances with the restricted data type can be created using the following given syntax. ... add(): add() method used to insert elements in the queue.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Programiz
programiz.com › dsa › queue
Queue Data Structure and Implementation in Java, Python and C/C++
We usually use arrays to implement queues in Java and C/++. In the case of Python, we use lists.
🌐
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.
🌐
CodeSignal
codesignal.com › learn › courses › mastering-complex-data-structures-in-java › lessons › stacks-and-queues-in-java
Stacks and Queues in Java
A queue operates on the "First In, First Out" or FIFO principle, similar to waiting in line. In Java, we can implement a queue using the Queue<E> interface with a LinkedList or ArrayDeque, where add (enqueue) inserts at the end and remove or poll (dequeue) removes from the front.
🌐
Computer Science Circles
cscircles.cemc.uwaterloo.ca › java_visualize
Java Visualizer
The visualizer supports StdIn, StdOut, most other stdlib libraries, Stack, Queue, and ST. Click for FAQ. How can I access Java's built-in Stack/Queue instead of the introcs one?
🌐
WsCube Tech
wscubetech.com › resources › java › queue
Java Queue Interface: Methods, Examples, Features
September 2, 2025 - Master the Java Queue Interface with detailed methods, features, and examples. Get step-by-step explanations of implementations, uses, and more. Read now!