Stack Class in Java

In this article, we will learn about Stack Class in Java.

The Java Collection Framework provides a stack class that models and implements stack data structures. That is based on the “last in, first out” LIFO principle. In addition to basic push and pop operations, the class offers three other functions: dump, find and peek. You can also say that the class extends Vector and treats the class as a stack with the five functions mentioned. The class can refer to as a subclass of Vector. The following diagram shows the hierarchy of stack classes:

The class supports a standard Stack () constructor, which is used to create an empty stack.

Declaration:

public class Stack<E> extends Vector<E>

All interfaces implemented

  • Serializable:
    This is a marker interface in which classes must implement if they are to be serialized and deserialized.
  • Cloneable:
    This is an interface in Java that a class must implement in order for its objects to be cloned.
  • Iterable:
    This interface represents a collection of iterable, that is, iterable objects.
  • Collection:
    A Collection represents a group of objects called its elements. The collection interface passes the collections of objects for which maximum generality is desire.
  • List:
    The List interface offers the possibility of saving the ordered collection. This is the child interface of the collection.
  • RandomAccess:
    This is a markup interface used by List implementations to indicate that they support fast (usually constant) random access.

How to create a stack?

We need to import the java.util.stack package to create a stack and use the Stack () constructor of that class. The following example creates an empty stack. Here E is a type of object.

Stack<E> stack = new Stack<E>(); 

Example

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

class stack
{
	// Pushing element on the top of the stack
	static void stack_push(Stack<Integer> stack)
	{
		for(int i = 0; i < 5; i++)
		{
			stack.push(i);
		}
	}
	
	// Popping element from the top of the stack
	static void stack_pop(Stack<Integer> stack)
	{
		System.out.println("Pop Operation in Stack:");

		for(int i = 0; i < 5; i++)
		{
			Integer y = (Integer) stack.pop();
			System.out.println(y);
		}
	}
 
	static void stack_peek(Stack<Integer> stack)
	{
		Integer element = (Integer) stack.peek();
		System.out.println("Element on the stack top : " + element);
	}
	 
	static void stack_search(Stack<Integer> stack, int element)
	{
		Integer pos = (Integer) stack.search(element);

		if(pos == -1)
			System.out.println("Element is not found");
		else
			System.out.println("Element is found at position: " + pos);
	}
 
	public static void main (String[] args)
	{
		Stack<Integer> stack = new Stack<Integer>();

		stack_push(stack);
		stack_pop(stack);
		stack_push(stack);
		stack_peek(stack);
		stack_search(stack, 2);
		stack_search(stack, 6);
	}
}

Output:

Pop Operation:
8
6
4
2
0
Element on stack top: 8
Element is found at position: 3
Element not found

That’s all, we will learn about Stack Class in Java.

Next Article

1. PriorityQueue in Java

Please share this post:
Posts created 72

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: