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 - Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll(). In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties. The offer method inserts an element if possible, otherwise returning false.
🌐
GeeksforGeeks
geeksforgeeks.org › java › queue-interface-java
Queue Interface In Java - GeeksforGeeks
LinkedList: Implements List and Deque interfaces, allows null elements, and can be used as a FIFO queue when used through the Queue interface.
Published   2 weeks ago
🌐
Oracle
docs.oracle.com › javase › tutorial › collections › implementations › queue.html
Queue Implementations (The Java™ Tutorials > Collections > Implementations)
The head of the queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements; ties are broken arbitrarily. PriorityQueue and its iterator implement all of the optional methods of the Collection and Iterator interfaces.
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()
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-queue
Java Queue – Queue in Java | DigitalOcean
August 4, 2022 - Java Queue interface extends Collection interface. Collection interface extends Iterable interface. Some of the frequently used Queue implementation classes are LinkedList, PriorityQueue, ArrayBlockingQueue, DelayQueue, LinkedBlockingQueue, PriorityBlockingQueue etc…
🌐
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.
🌐
Programiz
programiz.com › java-programming › queue
Java Queue Interface
The Queue interface includes all the methods of the Collection interface.
🌐
CodeGym
codegym.cc › java blog › java collections › java queue interface and implementations
Java Queue Interface and implementations
AbstractQueue according to Queue ... 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
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 - We will implement the same methods enqueue, dequeue, front, and display in this program. The difference is that we will be using the Linked List data structure instead of Array. The below program demonstrates the Linked List implementation of ...
🌐
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 › interfaces › queue.html
The Queue Interface (The Java™ Tutorials > Collections > Interfaces)
The remove and poll methods both remove and return the head of the queue. Exactly which element gets removed is a function of the queue's ordering policy. The remove and poll methods differ in their behavior only when the queue is empty.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › Queue.html
Queue (Java Platform SE 7 )
Whatever the ordering used, the head of the queue is that element which would be removed by a call to remove() or poll(). In a FIFO queue, all new elements are inserted at the tail of the queue. Other kinds of queues may use different placement rules. Every Queue implementation must specify its ordering properties. The offer method inserts an element if possible, otherwise returning false.
🌐
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.
🌐
Princeton University
algs4.cs.princeton.edu › 13stacks › Queue.java.html
Queue.java
February 8, 2023 - * * @return an iterator that iterates over the items in this queue in FIFO order */ public Iterator<Item> iterator() { return new LinkedIterator(first); } // a linked-list iterator private class LinkedIterator implements Iterator<Item> { private Node<Item> current; public LinkedIterator(Node<Item> first) { current = first; } public boolean hasNext() { return current != null; } public Item next() { if (!hasNext()) throw new NoSuchElementException(); Item item = current.item; current = current.next; return item; } } /** * Unit tests the {@code Queue} data type.
🌐
CodeJava
codejava.net › java-core › collections › java-queue-collection-tutorial-and-examples
Java Queue Collection Tutorial and Examples
June 14, 2019 - The following code snippet illustrates how to iterate a linked list using the enhanced for loop: Queue<String> queueNames = new LinkedList<>(); queueNames.add("Dale"); queueNames.add("Bob"); queueNames.add("Frank"); queueNames.add("Alice"); queueNames.add("Eric"); queueNames.add("Cole"); queueNames.add("John"); for (String name : queueNames) { System.out.println(name); }Output: Dale Bob Frank Alice Eric Cole JohnMore simply, using Lambda expression with forEach() method in Java 8:
🌐
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.
🌐
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.
🌐
CodingNomads
codingnomads.com › java-301-queues-in-java
Queues in Java
The first plate you place down (at the bottom of the stack) will be the last one you can retrieve from that stack. All of the methods of accessing, inserting, removing, and searching are the same for queues as they are for stacks.
🌐
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
🌐
Vaia
vaia.com › java queue interface
Java Queue Interface: Example & Techniques | Vaia
When using the Java Queue Interface, understanding its operations is crucial for managing data collections effectively. Here are common use cases: Managing requests in a system where tasks are handled one at a time. Storing elements to be processed in order. Implementing breadth-first search algorithms in graph traversal.In practice, you can use the LinkedList class to create a queue and work with its methods.