Java Stack is a legacy Collection class. It extends Vector class with five operations to support LIFO (Last In First Out). It is available in Collection API since Java 1.0.

As Vector implements List, Stack class is also a List implementation class but does NOT support all operations of Vector or List. As Stack supports LIFO, it is also known as LIFO Lists.

Java Stack

In this post we will discuss following concepts about Stack in java.

  1. Java Stack
  2. Java Stack Class Diagram
  3. Java Stack Methods
  4. Java Stack Basic Example
  5. How Stack’s push() and pop() works Internally?
  6. Java Array to Stack Example
  7. Java List to Stack Example
  8. Java Stack to List Example

Java Stack

Java Stack is LIFO object. It extends Vector class but supports only five operations. Java Stack class has only one constructor which is empty or default constructor. So, when we create a Stack, initially it contains no items that mean Stack is empty.

Stack internally has a pointer: TOP, which refers to the top of the Stack element. If Stack is empty, TOP refers to the before first element location. If Stack is not empty, TOP refers to the top element.

Java Stack Class Diagram

Java Stack extends Vector class directly and implements RandomAccess, List, Collection etc. interface indirectly. It is a LIFO list.

Stack-Class-Diagram-450x313

Java Stack Methods

Java Stack extends Vector class with the following five operations only.

  1. boolean empty(): Tests if this stack is empty.
  2. E peek(): Looks at the object at the top of this stack without removing it from the stack.
  3. E pop() : Removes the object at the top of this stack and returns that object as the value of this function.
  4. E push(E item) : Pushes an item onto the top of this stack.
  5. int search(Object o) : Returns the 1-based position where an object is on this stack.

We will discuss these operations in-depth in the coming sections with some useful examples.

Java Stack Example

It’s time to develop basic Java Stack example to explore it’s operations.

Output:

How Stack’s push() and pop() operations works Internally?

As we know Stack’s push() and pop() are most frequently used Stack operations. The push() operation is used to insert an element into a Stack at the top. The pop() operation is used to remove the top element from a Stack.

Stack data structure has one internal property: top to refer top element of that stack. If Stack is empty, this top refers to the before first element as shown below:

empty_stack-450x413

As shown in the below diagram, Stack’s Push operation always inserts new element at the top of the Stack.

empty_stack

As shown in the below diagram, Stack’s Pop operation always removes an element from the top of the Stack.

stack_pop_operation-450x188

Stack’s isEmpty() operation returns True if there are no elements, otherwise, it returns False.

Java Array to Stack Example

Let us explore on “How to create a Stack object with a given Int array” here.

Output:-

Java List to Stack Example

Let us explore on “How to create a Stack object with a given List of Integers” here.

Output:-

Java Stack to List Example

Let us explore on “How to create a List object with a Stack of Integers” here.

Output:-

That’s all of a quick roundup on Stack in Java. I hope these Java Stack basics and examples will help you in getting started with Stack programming.

Thank you for reading my tutorials. Please drop me a comment if you like my tutorials or have any issues or suggestions or any type errors.

Reference: API Doc

By admin

Leave a Reply