Open In App

Stack Java Library

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
8 Likes
Like
Report

In Java, there are multiple ways to use built-in libraries for Stack.

Stack Class

  • It is a legacy collection from early Java versions. It is outdated and rarely used in modern Java
  • It's synchronized and thread-safe, which can be slower in single-threaded applications like doing data structures and CP problems.
  • Synchronized by default, which might add unnecessary overhead if thread-safety isn't required.

ArrayDeque Class

  • Faster performance for single-threaded scenarios.
  • Resizable array-backed without the overhead of synchronization.
  • More flexible, as it can also be used as a queue.
  • Cannot store null elements (throws NullPointerException if attempted).

If you are not sure for a coding problem, you may always use this for fast performance.


Output
Stack after insertion: [15, 19, 17]
Stack after deletion: [19, 17]
Stack after deletion: [17]

LinkedList Class

  • Also implements Deque and can be used as a stack, queue, or double-ended queue. It is a doubly-linked list implementation.
  • We use LinkedList when we want to leverage linked list operations like insertions and deletions are required at both ends.
  • More memory overhead as we used linked list.
FeatureStackArrayDequeLinkedList
SynchronizationYesNoNo
Backing StructureArray (Vector)Resizable ArrayDoubly-Linked List
Null ElementsYesNoYes
Use as QueueNoYesYes
PerformanceSlowerFastestModerate
Memory OverheadModerateLowHigh
Thread SafetySynchronizedNot thread-safeNot thread-safe

Article Tags :
Practice Tags :

Explore