Skip to content

[Pattern] Stack to Deque and ArrayDeque #179

Description

@brunoborges

Category

collections

Slug

stack-to-deque

Title

Stack to Deque and ArrayDeque

Difficulty

beginner

Since JDK

6

Summary

Use the Deque interface with ArrayDeque instead of the legacy Stack class.

Old code label

Legacy Stack

Old code

Stack<String> stack = new Stack<>();
stack.push("task");
String next = stack.pop();

Modern code label

Deque API

Modern code

Deque<String> stack = new ArrayDeque<>();
stack.push("task");
String next = stack.pop();

Explanation

Stack extends the legacy synchronized Vector class. Deque models stack operations directly, and ArrayDeque is the preferred general-purpose implementation for in-memory LIFO work.

Why the modern way wins

🎯 Better abstraction — Deque explicitly models both stack and queue operations.
⚡ Lower overhead — ArrayDeque avoids Vector's legacy synchronization.
🧩 More flexible — The same interface supports LIFO and FIFO algorithms.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    slugNew or updated pattern snippet (category/slug.json)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions