Stack, Queue, Deque - Feature ImageStack, Queue, Deque - Feature Image
HappyCoders Glasses

Stack, Queue, Deque in Java

Sven Woltmann
Sven Woltmann
Last update: November 27, 2024

Sooner or later, Java developers have to deal with the abstract data type queue, deque, and stack. In the stack, queue and deque tutorials, you will find answers to the following questions:

  • How do the queue, deque, and stack data structures work in general?
  • How do they differ?
  • How do the Java interfaces and classes Stack, Queue and Deque differ?
  • Which queue, deque, and stack implementations are provided by the JDK?
  • Which of the numerous implementations are suitable for which purposes?
  • How to implement queues, deques and stacks yourself?

You can find all code examples in the "Java Collections Guide" GitHub repository.

Data structures: What are stacks, queues, and deques?

A stack is a list of elements in which elements are inserted ("stacked") and removed on the same side (in representations classically at the top):

Stack vs. queue vs. deque: stack data structure
Stack data structure

For more details, see the main article about the stack data structure.

A queue is a list of elements where elements are inserted on one side and removed in the same order on the other side:

Stack vs. queue vs. deque: queue data structure
Queue data structure

You can learn everything about queues in the main article about the queue data structure.

A deque (Double-ended queue, pronounced "deck") is a list of elements where the elements can be inserted and removed both on one side and on the other:

Queue vs. Deque: deque data structure
Deque data structure

For details, see the main article about the deque data structure.

How do Stack, Queue, and Deque Differ?

The differences between the respective data structures are explained in the following articles:

What Java Implementations Are Available, and Which Should You Use?

The usage recommendations are based on the characteristics of the JDK queue and deque implementations, which are described in more detail in the linked articles.

The following are my recommendations for general purpose use:

The following queues are for special purposes:

If you still have questions, please ask them via the comment function. Do you want to be informed about new tutorials and articles? Then click here to sign up for the HappyCoders.eu newsletter.