Skip to content

Difference Between Stack and Queue (Data Structures and Java)

The same five spheres in red, orange, yellow, green and blue against sand: stacked in an upright glass tube, with the blue one on top lifting out; lined up in a horizontal glass tube, with the red one at the front rolling out on the right

In this article, you’ll learn:

  • What are the differences between stack and queue data structures?
  • What do the LIFO principle and FIFO principle mean?
  • When do you use a stack, and when a queue?
  • How do the Java class Stack and the Java interface Queue differ?

Let’s start with the data structures.

Difference between Stack and Queue

A stack is a linear data structure where the elements are inserted and removed according to the LIFO principle (“last-in-first-out”). That means that the element placed on the stack last is the first to be removed – and the element placed on the stack first is removed last.

Stack data structure: push places the element “grape” on top of the stack, pop removes it from there
Stack: the element inserted last is removed first

A queue is a linear data structure in which the elements are inserted and removed according to the FIFO principle (“first-in-first-out”). The first elements to be inserted in the queue are also the first to be removed, and the elements inserted last are removed last.

Queue data structure: enqueue inserts the element 14 at the tail of the queue, dequeue removes the element 5 at the head
Queue: elements are inserted at the tail and removed at the head

For more details, such as areas of application and considerations of time complexity, see the main article on the stack data structure and the main article on the queue data structure.

Stack and Queue – Terminology

The insertion and removal operation as well as the sides of the data structures are named differently for stacks and queues:

OperationStackQueue
InsertPush (top)Enqueue (back / tail)
RemovePop (top)Dequeue (front / head)

The “bottom” of the stack is not accessible via the operations.

When to Use a Stack, When a Queue?

The choice follows from the order in which the elements are to be processed:

  • A stack fits when the most recently added element is to be processed first: the back history of a browser tab, the call stack with the return addresses of method calls, or the processing of nested structures such as XML and JSON documents in parsers.
  • A queue fits when the elements are to be processed in order of arrival: the printer queue, or HTTP requests that a web server buffers while all threads of its thread pool are busy.

You’ll find more detailed examples in the two main articles linked above.

Difference between Java Stack and Queue

This section describes the differences between the Java class java.util.Stack and the interface java.util.Queue concerning various aspects.

Class vs. Interface

Stack is a class (→ all details about the Stack class), i.e., a concrete implementation of the stack data type in the JDK.

Queue, on the other hand, is an interface (→ all details about the Queue interface). The JDK provides several queue implementations with different characteristics. You can choose a suitable queue implementation according to your application area.

Methods

In Java, the operations from the terminology table are named as follows:

OperationStackQueue (throws exception)Queue (returns special value)
Insertpush()add()offer()
Removepop()remove()poll()
Examinepeek()element()peek()

Queue offers each operation in two variants: One throws an exception if the queue is empty (or, when inserting, full); the other returns null or false instead. Stack has only one variant – pop() and peek() throw an EmptyStackException if the stack is empty.

Thread Safety

All Stack operations go through synchronized methods (its own or those of its superclass Vector) – Stack is, therefore, thread-safe.

However, if we do not need thread safety, synchronization is unnecessary.

And if we need thread safety, the use of pessimistic locking, as synchronized uses it, would only make sense for a high number of access conflicts (“high thread contention”). For moderate access conflicts, optimistic locking would be more appropriate.

For the Queue interface, the JDK offers several implementations:

Stack or Deque?

The JDK developers recommend not using the Stack class anymore and using implementations of the Deque interface instead, which also defines the stack methods push(), pop(), and peek().

The JDK also offers numerous implementations for the Deque interface:

Violation of the Interface Segregation Principle

Both the Stack class and the Deque interface define methods that the respective data structure should not offer. Thus, both violate the interface segregation principle.

Since Stack implements the Collection interface and Deque extends it, both have methods such as remove(), removeIf(), removeAll(), and retainAll() that can be used to remove elements from the middle of the data structure. These are not merely declared methods – they work on Stack just as on all Deque implementations in the JDK.

Stack also has an insertElementAt() method that we can use to insert elements in the middle of the stack. Since Java 21, the SequencedCollection interface adds addFirst() on top of that – for Stack, this method inserts at the bottom, i.e., exactly where a stack is, by definition, not accessible. Deque has the counterpart: When an ArrayDeque is used as a stack, the top is at the front, and addLast() inserts at the bottom.

The section “Stack Interface” and the article “Implementing a Queue using an Array” show what a Stack and Queue interface should look like.

Summary

The most important differences between stack and queue at a glance:

  • A stack works according to the LIFO principle, a queue according to the FIFO principle.
  • In Java, Stack is a concrete class; Queue is an interface with several implementations from which you choose depending on your use case.
  • Stack is always synchronized; with Queue, the implementation you pick decides whether and how it synchronizes.
  • Both violate the interface segregation principle; as a stack, you should therefore use a Deque implementation such as ArrayDeque.

To learn how the Queue and Deque interfaces differ, read the article “Java Queue vs. Deque”.

If this article has helped you, I would greatly appreciate a positive review on my ProvenExpert profile. Your feedback helps me improve my content and motivates me to write new informative articles.

👉 Leave a review

Want Even More Knowledge?

My blog features many articles on Java, software architecture, and performance — from foundational concepts to advanced patterns.

If you want to go deeper, check out my trainings: hands-on, easy to understand, and directly applicable to your day-to-day project work. Instead of theory, I teach principles that help you write code that is better, more maintainable, and more performant in the long run.

Explore the Java Trainings

Become a Better Java Developer

My free newsletter keeps you ahead. Modern Java: new versions & features, performance, and JVM insights – once a month.

Search