Last Updated:

Stack class in Java

Stack class in Java

In Java Stack, this is a class that falls under a collection structure that extends the Vector class. It is a stack of LIFO objects. Before using the Stack class, we must import the java.util package. The stack class is located in the hierarchy of the collection structure, as shown below.

Stack Class Designer

 

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

publicStack()

Create a stack

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

Stack stk = new Stack();

Methods of the Stack Class

 

We can perform pop, peek, push, and search operations on the stack. The Java stack class provides basically five methods for performing these operations. Along with this, it also provides the entire Java Vector class.

StackClassempty() method

 

The empty() method of the Stack class checks whether the stack is empty. If yes, it returns true, otherwise returns false. We can also use the isEmpty() method of the Vector class.

Syntax

publicbooleanempty():

The method returns true if the stack is empty, otherwise returns false.

The first time, it returns true because we haven't inserted a single element into the stack. After that, we push the elements into the stack.Call the empty () method again, which will give false because the stack is not empty.

Push() method of the stack class

The method inserts the item at the top of the stack. This works in the same way as describing the addelement(point) method of a vector class. It passes a parameter element that will be placed on the stack.

Syntax

public E push(E item)

The peek() method of the stack class

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

Syntax

publicE peek()

It returns the top elements of the stack. Let's look at an example of the peek() method.

importjava.util.Stack;
public class StackPeekMethodExample
{
public static void main(String[] args)
{
Stack<String>stk= new stack<>();
// pushing elements into
the stack stk.push("Apple");
stk.push("Grape");
stk.push("Mango");
stk.push ("Orange");
System.out.println ("stack:" + "STK");
// Accessing an item from the top of the
fruit line stack = stk.peek();
//stack printing
System.out.println ("Element from above:" + fruit);

}
}

Exit:

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

Stack class() search method

The method looks for an object in the stack at the top. It analyzes the parameter we want to find. It returns the location of the object in the stack based on 1. The topmost object of the stack is viewed at a distance of 1.

Suppose this is an object in the stack that we want to find. The method returns the distance from the top of the entry stack closest to the top of the stack. It uses the equals() method to find an object on the stack.

Syntax

publicintsearch(Object o)

o is the search object to search.

Gets the location of an object from the top of the stack. If it returns -1, it means that the object is not on the stack.

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

StackSearchMethodExample.java
importjava.util.Stack;
public class StackSearchMethodExample
{
public static void main(String[] args)
{
Stack<String>stk= new stack<>();
//Add elements to
the stack stk.push("Mac");
stk.push("HP");
stk.push("Dell");
stk.push("Asus");
System.out.println ("stack:" + "STK");
// Find the int elementlocation
= stk.search("HP");
System.out.println ("Dell Location:" + location);

}
}