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 - 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   5 days 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.
🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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
🌐
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); } }
🌐
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 ...
🌐
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
🌐
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.
🌐
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.
🌐
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.
🌐
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 …
🌐
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.
🌐
Oracle
docs.oracle.com › javase › tutorial › collections › interfaces › queue.html
The Queue Interface (The Java™ Tutorials > Collections > Interfaces)
See Java Language Changes for a summary of updated language features 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. A Queue is a collection for holding elements prior to processing.
🌐
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
🌐
Reddit
reddit.com › r/javahelp › java queue - creating my own methods
r/javahelp on Reddit: Java Queue - Creating my Own Methods
September 29, 2022 -

Hullo. I'm currently working on an assignment revolving around creating my own Queue methods through Linked List implementation. I've done an assignment that required me to do the same for Stacks (pop, push, printStack), so I (naively) went into this thinking I could reuse my code from before.

And I wasn't wrong for the printQueue and enQueue methods. I know that Queues work on a First In First Out basis, and I'm having trouble with my deQueue method, which removes and returns the first element. For example, with a queue consisting of [3,2,1], with the entry order being 1,2, then 3, the method should remove 1, then 2, then 3. For the most part, I have this part working up until the the last element of the queue, fails to get removed and instead my code throws a NullPointerException.

public E dequeue() {
        Node temp = head;
       
        if((head == null)){ //if the head is null, then the tail is too
            tail = null;
            System.out.println("The list is empty.");
            return null;
        }

        while(temp.next.next != null){
                temp = temp.next;
            }
        temp.next = null;



        return temp.data;
    }

The while loop iterates through the queue until it recognizes that it has reached the tail node. According to my debugger, an issue arises once I only have one node/element. I used that code for another assignment a few months back, but now it's giving me a major issue (and headache). Starting to think my implementation of temp.next.next is giving me the exception, though any help would be greatly appreciated!