A Stack is a Last In First Out (LIFO) data structure. In this tutorial, we will be discussing the Stack Class in Java, what are the methods in the stack class, how to create a java stack, and Stack Implementation with Example. Go through the direct links and learn the java stack class thoroughly.
This Stack Class in Java Tutorial Contains:
- Java Stack Class
- Declaring a Stack in Java
- Interfaces Implemented in Stack Declaration
- Stack Class Constructor
- Creating a Stack Class in Java
- Java Stack Methods
- Stack Implementation
- Example on Stack Class in Java
Java Stack Class
Stack in Java is a class that presents in java.util package. A stack is a child class of the vector that implements standard last-in,first-out(LIFO) stack data structure. It only defines the default constructor, which is used to create an empty stack. The stack includes all the methods defined by vector class and adds several of its own methods.
To put an element at the top of the stack we can use the push() method. To remove the top element we can use a pop() method. An EmptyStackException is thrown if you call the pop() method and your invoking stack is empty.
Also Refer:
Declaring a Stack in Java
public class Stack<E> extends Vector<E>
Interfaces Implemented in Stack Declaration
- Serializable: This is a marker interface that classes must perform if they are to be serialized and deserialized.
- Cloneable: This is an interface in Java that needs to be performed by a class to allow its objects to be cloned.
- List<E>: The List interface gives a way to store the ordered collection. The list is also a child interface of Collection.
- Iterable<E>: Iterable interface specifies a collection of objects which is iterable — meaning which can be iterated.
- RandomAccess: This is a marker interface used by List implementations to show that they support fast (generally constant time) random access.
- Collection<E>: A Collection describes a group of objects acknowledged as its elements. The Collection interface is used to pass around collections of objects where maximum generality is desired.
Stack Class Constructor
The Stack class includes only the default constructor that creates an empty stack. That is, as follows:
public Stack()
Creating a Stack Class in Java
If you don’t know how to create a stack in java then this section will help you a lot. Here, we are discussing creating a java stack class. To create a stack, it is necessary to import the java.util.Stack package. Follow the below syntax after importing the package.
Stack<Type> stacks = new Stack<>();
Here, Type indicates the stack’s type. For instance,
// Create Integer type stack Stack<Integer> stacks = new Stack<>(); // Create String type stack Stack<String> stacks = new Stack<>();
Methods of the Stack Class in Java
1. Object push(Object element): This method is used to push an element at the top of the stack.
2. Object pop(Object element): This method is used to pop the element at the top of the stack.
3. Object peek(): This method is used to returns the elements at the top of the stack, but doesn’t remove it.
4. boolean empty(): This method is used to check the stack is empty or not. If the stack is empty returns true, and it returns false if the stack is not empty.
5. int search(Object element): This method is used to search whether a particular element is available in a stack or not. If the element is found in the stack it returns the position of the element else it returns -1.
Stack Implementation
In the stack, elements are stored and obtained in the Last In First Out manner. In other words, elements are inserted to the top of the stack and removed from the top of the stack.
Example on Stack Class in Java
import java.util.*; class Person { //pushing element at the top of stack static void showPush(Stack st, int a) { st.push(a); System.out.println("Push(" +a + ")"); System.out.println("Stack: " +st); } //pop element from the stack static void showPop(Stack st) { System.out.print("Pop -> "); Integer a =st.pop(); System.out.println(a); System.out.println("Stack: " +st); } //displaying top element from stack static void showPeek(Stack st) { Integer b = (Integer) st.peek(); System.out.println("Element at the top of the stack is: " +b); } //searching element in stack static void showSearch(Stack st, int element) { Integer position = (Integer) st.search(element); if(position == -1) System.out.println("Element not found"); else System.out.println("Element is found at position " + position); } public static void main(String args[]) { Stack st = new Stack(); System.out.println("Stack: " +st); showPush(st,10); showPush(st,20); showPush(st,30); showPush(st,40); showPop(st); showPeek(st); showSearch(st,20); showPop(st); showPop(st); showPop(st); try { showPop(st); } catch(EmptyStackException e) { System.out.println("Empty Stack"); } } }
Output: