Master Stacks and Queues Java Code with Easy Examples

Robert Jul 13, 2026

In the realm of data structures, stacks and queues are two fundamental concepts that every Java developer should be familiar with. Both are abstract data types that represent collections designed for holding elements, but they differ in the order in which they add, remove, and access elements.

a flow diagram with several different types of items in each section, including numbers and symbols
a flow diagram with several different types of items in each section, including numbers and symbols

Before diving into the Java code for stacks and queues, let's briefly discuss these data structures and their real-world applications. A stack follows the Last-In-First-Out (LIFO) principle, which means the last element added to the stack is the first one to be removed. It's like a stack of plates; you can only take the top plate from the stack. Queues, on the other hand, follow the First-In-First-Out (FIFO) principle. It's like a line of people waiting for a bus; the person who gets in line first will be the first one to get on the bus.

Stack & Queue - Intro
Stack & Queue - Intro

Stacks in Java

In Java, the java.util.Stack class is a standard implementation of the stack data structure. It extends the Vector class and provides a last-in-first-out (LIFO) stack of objects.

Stack vs Queue in C Programming | LIFO vs FIFO
Stack vs Queue in C Programming | LIFO vs FIFO

However, it's essential to note that the java.util.Stack class is part of the Java Collections Framework, but it'sīŋŊ discouraged for use. It's recommended to use the java.util.deque interface (and its implementations) instead, as it provides more extensive functionality and is part of the Java Collections Frameworkgets more extensive functionality and is part of the Java Collections Framework.

Basic Stack Operations

🧱 JAVA STACK VS QUEUE (VISUAL COMPARISON!) 🔄
🧱 JAVA STACK VS QUEUE (VISUAL COMPARISON!) 🔄

Here's a simple Java example using the java.util.Deque interface for basic stack operations:


import java.util.Deque;
import java.util.ArrayDeque;

public class StackExample {
    public static void main(String[] args) {
        Deque<String> stack = new ArrayDeque<String>();
        stack.push("First");
        stack.push("Second");
        stack.push("Third");

        while (!stack.isEmpty()) {
            System.out.println(stack.pop());
        }
    }
}
This example demonstrates the following stack operations: - push: adds an element to the top of the stack - pop: removes and returns the top element of the stack - isEmpty: checks if the stack is empty

Stack Applications: Reverse a String

Learn Stack in C
Learn Stack in C

A classic use case for stacks is to reverse a string. Here's how you can achieve this:


public String reverseString(String s) {
    Deque<Character> stack = new ArrayDeque<Character>();
    for (char c : s.toCharArray()) {
        stack.push(c);
    }

    StringBuilder reversed = new StringBuilder();
    while (!stack.isEmpty()) {
        reversed.append(stack.pop());
    }

    return reversed.toString();
}

Queues in Java

The java.util.Queue interface is a standard implementation of the queue data structure. It extends the java.util.List interface and provides a first-in-first-out (FIFO) queue of objects.

Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained

There are multiple implementations of the java.util.Queue interface, such as java.util.LinkedList and java.util.PriorityQueue. The most straightforward implementation is java.util.LinkedList, which uses a doubly-linked list internally to store elements.

Basic Queue Operations

Stack & Queue
Stack & Queue
two different types of stack versus are shown in the diagram below, which one is for each
two different types of stack versus are shown in the diagram below, which one is for each
Data Structures With JavaScript: Stack and Queue | Envato Tuts+
Data Structures With JavaScript: Stack and Queue | Envato Tuts+
types of queues
types of queues
Stack vs Queue Explained Simply
Stack vs Queue Explained Simply
the full stack devloper is shown in orange and white, with text below it
the full stack devloper is shown in orange and white, with text below it
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Stack vs Queue Explained Simply | Data Structures Notes for CSE Students
Stack vs Queue Explained Simply | Data Structures Notes for CSE Students
📚 Data Structure Cheat Sheet for Beginners | Arrays, Linked List, Stack, Queue, Tree & Graph
📚 Data Structure Cheat Sheet for Beginners | Arrays, Linked List, Stack, Queue, Tree & Graph
Stack vs Queue Explained in 10 Seconds | LIFO vs FIFO (DSA Basics)
Stack vs Queue Explained in 10 Seconds | LIFO vs FIFO (DSA Basics)
Master Two Pointers Technique đŸ’ģ | Java DSA Interview Preparation Guide
Master Two Pointers Technique đŸ’ģ | Java DSA Interview Preparation Guide
Java Full Stack Developer Course
Java Full Stack Developer Course
Stacks and Queues
Stacks and Queues
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
☕ Java Operators Cheat Sheet | Master Java Operators Easily 🚀
Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |
Java Arrays Cheat Sheet (2026) | Complete Java Arrays Guide for Beginners & Students |
stack
stack
Different between Stack and Queue
Different between Stack and Queue
Data Structures Cheat Sheet for Beginners
Data Structures Cheat Sheet for Beginners
đŸ”Ĩ Save This Stack vs Queue Cheat Sheet Before Your DSA Exam!
đŸ”Ĩ Save This Stack vs Queue Cheat Sheet Before Your DSA Exam!

Here's a Java example using the java.util.Queue interface for basic queue operations:


import java.util.Queue;
import java.util.LinkedList;

public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<String>();
        queue.offer("First");
        queue.offer("Second");
        queue.offer("Third");

        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}
This example demonstrates the following queue operations: - offer: adds an element to the tail of the queue - poll: removes and returns the head element of the queue - isEmpty: checks if the queue is empty

Queue Applications: Print Even Indexed Numbers

A common interview question involves using queues to print even and odd indexed elements of an array in two separate threads. Here's an example of how you can achieve this using the java.util.concurrent.BlockingQueue:


//... (implement even thread, odd thread, and main class)

In conclusion, understanding stacks and queues is crucial for Java developers, as these data structures have wide-ranging applications in various aspects of programming. By mastering these fundamental concepts, you'll be well-equipped to tackle a broad range of coding challenges. So, keep practicing and exploring the intricacies of these data structures to enhance your Java skills!