Stack class in Java

Java Collection structure gives a Stack class which designs and also applies Heap data structure. The course is based upon the fundamental concept of last-in-first-out. Along with the standard push as well as pop procedures, the course supplies 3 even more functions of empty, search and peek. The class can also be stated to expand Vector and treats the course as a stack with the five discussed functions. The class can also be described as the subclass of Vector.
This layout shows the power structure of Heap course:


java stack

The course supports one default constructorStack() which is utilized to create a vacant stack.
Below program shows few fundamental operations supplied by the Heap class:

// Java code for stack implementation

import java.io.*;
import java.util.*;

class Test
// Pushing element on the top of the stack
static void stack_push(Stack<Integer> stack)
for(int i = 0; i < 5; i++)

// Popping element from the top of the stack
static void stack_pop(Stack<Integer> stack)
System.out.println(“Pop :”);

for(int i = 0; i < 5; i++)
Integer y = (Integer) stack.pop();

// Displaying element on the top of the stack
static void stack_peek(Stack<Integer> stack)
Integer element = (Integer) stack.peek();
System.out.println(“Element on stack top : ” + element);

// Searching element in the stack
static void stack_search(Stack<Integer> stack, int element)
Integer pos = (Integer) stack.search(element);

if(pos == -1)
System.out.println(“Element not found”);
System.out.println(“Element is found at position ” + pos);

public static void main (String[] args)
Stack<Integer> stack = new Stack<Integer>();

stack_search(stack, 2);
stack_search(stack, 6);


Java Stack, java.util.Stack, is a classical stack information structure. You can press aspects to the top of a Java Stack as well as pop them again, suggesting read and get rid of the aspects from the top of the pile.
The Java Stack course really executes the Java List, but you hardly ever utilize a Heap as a Listing – except probably if you need to evaluate all components presently stored on the stack.

Java Stack Essential

A Stack is a data framework where you include aspects to the “leading” of the pile, as well as likewise remove aspects from the top once more. This is additionally referred to as the “Last In First Out (LIFO)” principle. In contrast, a Java Queue utilizes a “First In First Out (FIFO)” concept, where aspects are included in completion of the line up, as well as gotten rid of from the get go of the line up.

Create a Stack

.To make use of a Java Stack you must first create a circumstances of the Heap course. Right here is an example of developing a Java Stack:
Stack stack = new Stack();

Push Element on Stack

As soon as you have a Java Stack circumstances, you can press elements to the top of the Stack. The elements you press onto the Heap must be Java things. Hence, you in fact push challenge the Stack.
You push aspects onto a Java Heap utilizing its press() method. Right here is an example of pushing an element (object) onto a Java Stack:

Pile stack = brand-new Heap();
stack.push(” 1″);

This Java instance pushes a Java String with the text 1 onto the Heap. The String 1 is after that kept on top of the Heap.

stack class java

Pop Aspect From Heap

When an aspect has actually been pressed onto a Java Heap, you can pop that element from the Stack again. Once an aspect is popped off the Heap, the element is gotten rid of from the Heap. The top element of theStack is then whatever component that was pressed onto the Heap right before the element just popped.
Pile utilizing the pop(). Here is an instance of popping a component off a Stack making use of the pop() technique:

Pile stack = brand-new Heap();
stack.push(” 1″);
String top Element = stack.pop();

When an aspect is popped off a Heap, the element is no longer present on the Stack.

Peek at Leading Aspect of Stack

The Java Stack class has a method called peek() which allows you to see what the leading element on the Stack is, without popping off the aspect. Right here is an example of peeking on top of a Java Stack:

Stack pile = brand-new Heap();
stack.push(” 1″);
String top Element = stack.peek();

After running this Java instance the topElement variable will consist of the String object 1 which was pressed onto the Heap right before peek() was called. The String object is still existing on the Heap after calling peek().

Search the Stack

You can search for an item on the pile to get it’s index, utilizing the search() technique. The item’s equals() technique is gotten in touch with every item on the Stack to identify if the searched-for item exists on the Stack. The index you obtain is the index from the topStack of the, implying the leading component on the Stack has index 1.
Below is exactly how you browse a Heap for an object:
Pile pile = new Heap();

stack.push(” 1″);
stack.push(” 2″);
stack.push(” 3″);

int index = stack.search(” 3″);// index = 1.

Repeat Aspects of Heap

You can iterate the elements of a Java Stack by getting a Java Iterator from the Heap. You obtain an Iterator by calling the Stack iterator() approach. Right here is an example of obtaining an Iterator from a Java Stack and iterating it:
Stack pile = new Heap();

stack.push(” 123″);
stack.push(” 456″);
stack.push(” 789″);

Iterator iterator = stack.iterator();
while( iterator.hasNext())
Object worth = iterator.next();

Process Stack Making Use Of Stream

It is likewise possible to refine the components on a Java Heap using the Java Stream API. You do so by first obtaining a Stream from the Stack through the stream() method.
As soon as you have acquired a Stream from the Stack, you can process the components in the stream. Right here is an example of obtaining a Stream from a Heap as well as processing the elements:

Pile stack = brand-new Heap();
stack.push(” A”);
stack.push(” B”);
stack.push(” C”);

Stream stream = stack.stream();

stream.forEach(( aspect) ->
System.out.println( component); );

Notice, that this example makes use of a Java Lambda as specification to the Stream.forEach() method. The lambda just publishes out the element to System.out.

Reverse Listing Using Stack

You can make use of a Java Heap to reverse a Java Checklist. You do so by pressing all the aspects from the Listonto the Heap, beginning with the element with index 0, then 1 etc. Each element is gotten rid of from the List, then pushed onto the Heap. Once all the components get on the Stack, you pop the components off individually and add them back to the vacant listing. Right here is an instance of turning around a Java Listing utilizing a Java Heap:

List checklist = new ArrayList();
list.add(” A”);
list.add(” B”);
list.add(” C”);
System.out.println( checklist);

Pile pile = new Heap();
while( list.size() > 0)

while( stack.size() > 0)

System.out.println( checklist);

Pile Use Situations

A Stack is really helpful for some types of data processing, as an example if you are analyzing an XML data utilizing either SAX or StAX.

stacks in java

Stack class in Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top