Java — Stack Class | Code Factory

Donate : Link

WordPress Blog : Link

A Stack is a Last In First Out (LIFO) data structure. It supports two basic operations called push and pop. The push operation adds an element at the top of the stack, and the pop operation removes an element from the top of the stack.

Java provides a Stack class which models the Stack data structure. The Stack class is part of Java’s collections framework. Following is the class hierarchy of Stack in Java -

The Stack class extends Vector which implements the List interface. A Vector is a re-sizable collection. It grows its size to accommodate new elements and shrinks the size when the elements are removed.

Since the Stack class extends Vector, it also grows and shrinks its size as needed when new elements are added or removed.

Push, Pop and Peek Operations :

Output :

Example 2 :

Output :

Example 3 :

Output :

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store