Java Stack

The stack is a linear data structure that is used to store the collection of objects. It is based on Last-In-First-Out (LIFO). Java collection framework provides many interfaces and classes to store the collection of objects. One of them is the Stack class that provides different operations such as push, pop, search, etc.

In this section, we will discuss the Java Stack class, its methods, and implement the stack data structure in a Java program. But before moving to the Java Stack class have a quick view of how the stack works.

The stack data structure has the two most important operations that are push and pop. The push operation inserts an element into the stack and pop operation removes an element from the top of the stack. Let's see how they work on stack.

Java Stack

Let's push 20, 13, 89, 90, 11, 45, 18, respectively into the stack.

Java Stack

Let's remove (pop) 18, 45, and 11 from the stack.

Java Stack

Empty Stack: If the stack has no element is known as an empty stack. When the stack is empty the value of the top variable is -1.

Java Stack

When we push an element into the stack the top is increased by 1. In the following figure,

  • Push 12, top=0
  • Push 6, top=1
  • Push 9, top=2
Java Stack

When we pop an element from the stack the value of top is decreased by 1. In the following figure, we have popped 9.

Java Stack

The following table shows the different values of the top.

Java Stack

Java Stack Class

In Java, Stack is a class that falls under the Collection framework that extends the Vector class. It also implements interfaces List, Collection, Iterable, Cloneable, Serializable. It represents the LIFO stack of objects. Before using the Stack class, we must import the java.util package. The stack class arranged in the Collections framework hierarchy, as shown below.

Java Stack

Stack Class Constructor

The Stack class contains only the default constructor that creates an empty stack.

Creating a Stack

If we want to create a stack, first, import the java.util package and create an object of the Stack class.

Or

Where type denotes the type of stack like Integer, String, etc.

Methods of the Stack Class

We can perform push, pop, peek and search operation on the stack. The Java Stack class provides mainly five methods to perform these operations. Along with this, it also provides all the methods of the Java Vector class.

Method Modifier and Type Method Description
empty() boolean The method checks the stack is empty or not.
push(E item) E The method pushes (insert) an element onto the top of the stack.
pop() E The method removes an element from the top of the stack and returns the same element as the value of that function.
peek() E The method looks at the top element of the stack without removing it.
search(Object o) int The method searches the specified object and returns the position of the object.

Stack Class empty() Method

The empty() method of the Stack class check the stack is empty or not. If the stack is empty, it returns true, else returns false. We can also use the isEmpty() method of the Vector class.

Syntax

Returns: The method returns true if the stack is empty, else returns false.

In the following example, we have created an instance of the Stack class. After that, we have invoked the empty() method two times. The first time it returns true because we have not pushed any element into the stack. After that, we have pushed elements into the stack. Again we have invoked the empty() method that returns false because the stack is not empty.

StackEmptyMethodExample.java

Output:

Is the stack empty? true
Elements in Stack: [78, 113, 90, 120]
Is the stack empty? false

Stack Class push() Method

The method inserts an item onto the top of the stack. It works the same as the method addElement(item) method of the Vector class. It passes a parameter item to be pushed into the stack.

Syntax

Parameter: An item to be pushed onto the top of the stack.

Returns: The method returns the argument that we have passed as a parameter.

Stack Class pop() Method

The method removes an object at the top of the stack and returns the same object. It throws EmptyStackException if the stack is empty.

Syntax

Returns: It returns an object that is at the top of the stack.

Let's implement the stack in a Java program and perform push and pop operations.

StackPushPopExample.java

Output:

stack: []
push -> 20
stack: [20]
push -> 13
stack: [20, 13]
push -> 89
stack: [20, 13, 89]
push -> 90
stack: [20, 13, 89, 90]
push -> 11
stack: [20, 13, 89, 90, 11]
push -> 45
stack: [20, 13, 89, 90, 11, 45]
push -> 18
stack: [20, 13, 89, 90, 11, 45, 18]
pop -> 18
stack: [20, 13, 89, 90, 11, 45]
pop -> 45
stack: [20, 13, 89, 90, 11]
pop -> 11
stack: [20, 13, 89, 90]

Stack Class peek() Method

It looks at the element that is at the top in the stack. It also throws EmptyStackException if the stack is empty.

Syntax

Returns: It returns the top elements of the stack.

Let's see an example of the peek() method.

StackPeekMethodExample.java

Output:

Stack: [Apple, Grapes, Mango, Orange]
Element at the top of the stack: Orange

The method searches the object in the stack from the top. It parses a parameter that we want to search for. It returns the 1-based location of the object in the stack. Thes topmost object of the stack is considered at distance 1.

Suppose, o is an object in the stack that we want to search for. The method returns the distance from the top of the stack of the occurrence nearest the top of the stack. It uses equals() method to search an object in the stack.

Syntax

Parameter: o is the desired object to be searched.

Returns: It returns the object location from the top of the stack. If it returns -1, it means that the object is not on the stack.

Let's see an example of the search() method.

StackSearchMethodExample.java

Java Stack Operations

Size of the Stack

We can also find the size of the stack using the size() method of the Vector class. It returns the total number of elements (size of the stack) in the stack.

Syntax

Let's see an example of the size() method of the Vector class.

StackSizeExample.java

Output:

Is the stack empty or not? false
The stack size is: 5

Iterate Elements

Iterate means to fetch the elements of the stack. We can fetch elements of the stack using three different methods are as follows:

  • Using iterator() Method
  • Using forEach() Method
  • Using listIterator() Method

Using the iterator() Method

It is the method of the Iterator interface. It returns an iterator over the elements in the stack. Before using the iterator() method import the java.util.Iterator package.

Syntax

Let's perform an iteration over the stack.

StackIterationExample1.java

Output:

BMW
Audi
Ferrari
Bugatti
Jaguar

Using the forEach() Method

Java provides a forEach() method to iterate over the elements. The method is defined in the Iterable and Stream interface.

Syntax

Let's iterate over the stack using the forEach() method.

StackIterationExample2.java

Output:

Iteration over the stack using forEach() Method:
119
203
988

Using listIterator() Method

This method returns a list iterator over the elements in the mentioned list (in sequence), starting at the specified position in the list. It iterates the stack from top to bottom.

Syntax

Parameter: The method parses a parameter named index.

Returns: This method returns a list iterator over the elements, in sequence.

Exception: It throws IndexOutOfBoundsException if the index is out of range.

Let's iterate over the stack using the listIterator() method.

StackIterationExample3.java

Output:

Iteration over the Stack from top to bottom:
988
203
119

Next TopicJava Tutorial




Contact US

Email:[email protected]

Java Stack
10/30