Difference Between Queue and Stack in Java

1. Introduction

In Java, Queue and Stack are two distinct data structures used for storing and managing a collection of elements. A Queue typically follows the FIFO (First-In-First-Out) principle, meaning the first element added to the queue will be the first one to be removed. Conversely, a Stack follows the LIFO (Last-In-First-Out) principle, where the last element added is the first to be removed.

2. Key Points

1. Queue operates on a FIFO (First In, First Out) basis, which is like a line at a grocery store.

2. Stack operates on a LIFO (Last In, First Out) basis, much like a stack of plates.

3. Queue is an interface in Java and can be implemented using various classes like LinkedList or PriorityQueue.

4. Stack is a class that extends Vector and provides methods for LIFO access, but it is considered to be a legacy class in Java.

3. Differences

Queue Stack
A collection designed for holding elements before processing. A collection designed for storing elements in a LIFO (Last In, First Out) manner.
Follows the FIFO (First In, First Out) principle. Elements are added to the end and removed from the beginning. Elements are added and removed from the top of the stack.
In Java, Queue is an interface in the java.util package. The Stack class is a traditional stack implementation in the java.util package, but it extends Vector and is considered somewhat legacy. Java recommends using Deque as a stack.
Common implementations include LinkedList, PriorityQueue, and ArrayDeque. For stack purposes, ArrayDeque (which implements the Deque interface) is recommended over the legacy Stack class.
Offers methods like add(), offer(), remove(), poll(), element(), and peek() for manipulating elements. Provides methods like push(), pop(), peek(), empty(), and search() to manipulate and inspect the stack.
This is used in scenarios where order matters and elements need to be processed in the order they were added. This is used in scenarios requiring backtracking, undo mechanisms, or when the last added element needs to be accessed first.

4. Example

// Importing necessary classes
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class DataStructureComparison {
    public static void main(String[] args) {
        // Step 1: Create a Queue using LinkedList
        Queue<Integer> queue = new LinkedList<>();

        // Step 2: Create a Stack
        Stack<Integer> stack = new Stack<>();

        // Step 3: Add elements to the Queue
        for(int i = 1; i <= 3; i++) {
            queue.add(i);
        }

        // Step 4: Add elements to the Stack
        for(int i = 1; i <= 3; i++) {
            stack.push(i);
        }

        // Step 5: Print the head of the Queue and the top of the Stack
        System.out.println("Head of Queue: " + queue.peek());
        System.out.println("Top of Stack: " + stack.peek());

        // Step 6: Remove elements from the Queue and Stack
        queue.remove();
        stack.pop();

        // Step 7: Print the elements after removal
        System.out.println("Elements in Queue after removal: " + queue);
        System.out.println("Elements in Stack after removal: " + stack);
    }
}

Output:

Head of Queue: 1
Top of Stack: 3
Elements in Queue after removal: [2, 3]
Elements in Stack after removal: [1, 2]

Explanation:

1. A Queue is implemented using a LinkedList, and elements are added to it.

2. A Stack is instantiated, and the same elements are added to it.

3. The first element of the Queue (the head) and the last element added to the Stack (the top) are displayed.

4. An element is removed from both the Queue and the Stack, showing the FIFO nature of the Queue and the LIFO nature of the Stack.

5. After removal, the Queue no longer contains the first element, and the Stack no longer contains the last element that was added.

5. When to use?

Use a Queue when you need to manage elements in a first-come-first-serve fashion, such as scheduling tasks, managing operations in a service-oriented architecture, or breadth-first search algorithms.

Use a Stack to manage tasks in a last-come-first-serve manner, which is useful for depth-first search algorithms, parsing expressions, backtracking algorithms, and maintaining function calls (call stack).

Comments