Stack is data-structure used to store elements/objects in it. Stack works on the LIFO i.e Last In First Out which means, an element which is added in the last to the stack is fetched first when retrieved from it. Let’s understand with example.
In stack we will be adding [1,44,54,20,10]
in the same sequence. Initially our stack will look something like this. When no elements in it.
After Adding elements it would like something like this.
Here we saw whenever we add 1, then 1 is stacked and after that when we add 44 in stack then, 44 is stacked on top of 1, and similarly all coming element to stack are stacked on top of one already presents.
And when we remove elements from the Stack it is removed from top of the stack. Means in our case if try to remove element from the stack then 10 is popped out first then 20 and then 54 and so on as shown below.
As in Stack, Last element is the first one to get removed when we try to remove an element from the Stack. That’s why Stack is said be of having LIFO architecture.
Table of Contents
Stack Operations
Stack has several dedicated operations. Let’s discuss one by one.
Push | Adding elements in Stack is termed as Push. i.e when we add 44 to Stack then it is called as push 44 to Stack |
POP | When we remove an element from Stack then this operation is termed is pop. Let’s remove 44 from Stack |
Peek | It uses to fetch the top elements of the stack but this won’t delete the element. |
Empty | Return true if no elements are present in stack or returns false |
Stack In Java
Java Stack is part of Java Collection
and is part of java.util
package. Stack in java extends Vector
Class and provides five methods that will provide required operations of a Stack.
Classes Extended By Stack
Stack extends multiple Classes
java.lang.Object java.util.AbstractCollection<E> java.util.AbstractList<E> java.util.Vector<E> java.util.Stack<E>
Interfaces Implemented By Stack
Serializable Cloneable Iterable<E> Collection<E> List<E> RandomAccess
Constructor in Stack
stack()
is only non parametrised constructor
Stack<String> stack = new Stack<>();
Methods in Stack
Java Stack provides five methods to perform stack operation.
Add element/item to Stack
Stack class in Java provides push(T item)
method to push elements in Java Stack.
E push(E items) | Add items to stack and returns the added value. |
public void createStringStack() { Stack<String> stack = new Stack<>(); System.out.println("Adding "+stack.push("India")+" to Stack"); System.out.println("Adding "+stack.push("America")+" to Stack"); System.out.println("Adding "+stack.push("China")+" to Stack"); System.out.println("Adding "+stack.push("Russia")+" to Stack"); System.out.println("Elements in stack after adding to Stack"+ stack); }
Adding Object to Stack in Java
public void createObjectStack() { Stack<Country> stack = new Stack<>(); stack.push(new Country("India")); stack.push(new Country("America")); stack.push(new Country("China")); stack.push(new Country("Russia")); }
Remove items from Stack in Java
Stack in java provides pop()
methods to remove elements from Stack.
E pop() | Removes an element from top of the stack and returns the removed value. |
public static void createStringStack() { Stack<String> stack = new Stack<>(); System.out.println("Adding "+stack.push("India")+" to Stack"); System.out.println("Adding "+stack.push("America")+" to Stack"); System.out.println("Adding "+stack.push("China")+" to Stack"); System.out.println("Adding "+stack.push("Russia")+" to Stack"); System.out.println("Elements in stack after adding to Stack"+ stack); System.out.println("Removing element "+ stack.pop()+" from Stack"); System.out.println("Removing element "+ stack.pop()+" from Stack"); }
OutPut of Above code:
Adding India to Stack Adding America to Stack Adding China to Stack Adding Russia to Stack Elements in stack after adding to Stack[India, America, China, Russia] Removing element Russia from Stack Removing element China from Stack
Getting Top element from the Stack
For getting top element from stack in java, Java provides peek()
method.
E peek() | Returns the top element from the stack without deleting it from stack. |
public static void stackPeek() { Stack<String> stack = new Stack<>(); System.out.println("Adding "+stack.push("India")+" to Stack"); System.out.println("Adding "+stack.push("America")+" to Stack"); System.out.println("Adding "+stack.push("China")+" to Stack"); System.out.println("Adding "+stack.push("Russia")+" to Stack"); System.out.println("Elements in stack after adding to Stack"+ stack); System.out.println("Element at peek is " + stack.peek()+ " in stack"); }
Adding India to Stack Adding America to Stack Adding China to Stack Adding Russia to Stack Elements in stack after adding to Stack[India, America, China, Russia] Element at peek is Russia in stack
Check if stack is empty or not
Stack provide empty()
method to check if stack is empty or not.
boolean empty() | Returns true if stack is empty else false |
public static void emptyStack() { Stack<String> stack = new Stack<>(); System.out.println("Adding "+stack.push("India")+" to Stack"); System.out.println("Adding "+stack.push("America")+" to Stack"); System.out.println("Adding "+stack.push("China")+" to Stack"); System.out.println("Adding "+stack.push("Russia")+" to Stack"); System.out.println("Elements in stack after adding to Stack"+ stack); System.out.println("Is stack empty " + stack.isEmpty()); }
Output of the above code
Adding India to Stack Adding America to Stack Adding China to Stack Adding Russia to Stack Elements in stack after adding to Stack[India, America, China, Russia] Is stack empty false
Search an element/ item in stack
Stack provides search(E item)
to search an element in Java Stack. This method return the index of item if found.
Stack follows 1 base indexing system.
int search(E item) | returns the index of item if found. else return -1. |
public static void searchStack() { Stack<String> stack = new Stack<>(); System.out.println("Adding "+stack.push("India")+" to Stack"); System.out.println("Adding "+stack.push("America")+" to Stack"); System.out.println("Adding "+stack.push("China")+" to Stack"); System.out.println("Adding "+stack.push("Russia")+" to Stack"); System.out.println("Elements in stack after adding to Stack"+ stack); System.out.println("Element India is present at index "+ stack.search("India")); System.out.println("Element Pakistan is present at index "+ stack.search("Pakistan")); }
Adding India to Stack Adding America to Stack Adding China to Stack Adding Russia to Stack Elements in stack after adding to Stack[India, America, China, Russia] Element India is present at index 4 Element Pakistan is present at index -1
Iterating over Stack items
We can iterate over Stack in java using Iterator()
which comes in from AbstractList
as Stack extends
AbstactList
public static void iterateStack() { Stack<String> stack = new Stack<>(); System.out.println("Adding "+stack.push("India")+" to Stack"); System.out.println("Adding "+stack.push("America")+" to Stack"); System.out.println("Adding "+stack.push("China")+" to Stack"); System.out.println("Adding "+stack.push("Russia")+" to Stack"); System.out.println("Elements in stack after adding to Stack"+ stack); System.out.println("Iterating of Stack in java"); Iterator<String> itert = stack.iterator(); while(itert.hasNext()) { System.out.print(itert.next()+ " "); } }
Adding India to Stack Adding America to Stack Adding China to Stack Adding Russia to Stack Elements in stack after adding to Stack[India, America, China, Russia] Iterating of Stack in java India America China Russia
Size of Stack in Java
As Stack extends AbstractList
class in java we can use size()
of the list to get size of Vector.
public static void stackSize() { Stack<String> stack = new Stack<>(); System.out.println("Adding "+stack.push("India")+" to Stack"); System.out.println("Adding "+stack.push("America")+" to Stack"); System.out.println("Adding "+stack.push("China")+" to Stack"); System.out.println("Adding "+stack.push("Russia")+" to Stack"); System.out.println("Elements in stack after adding to Stack"+ stack); System.out.print("Stack size is "+stack.size()); }
Adding India to Stack Adding America to Stack Adding China to Stack Adding Russia to Stack Elements in stack after adding to Stack[India, America, China, Russia] Stack size is 4
Conclusion
Stack is a data-structure work on LIFO architecture i.e first in last out. Stack in java extends classes like Collection
, Vector
, AbstractList
and implements interfaces like Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess.
Java Stack has five operations that are push, pop, peek, empty and search.
Thank’s Note
Thanks for spending you valuable time on this post hope this post was use full to you and you learned something new today. If you like our post please share our post.
References : Oracle Docs