Stack is most popular data structure in computer. Stack in Java collection framework is a class that extends Vector class. Stack is a class implements Serializable, Clonneable,Iterable<>, Collection<>, RAndomAccess and List<> intefaces.
Constructor
Stack has only one constructor.
Stack(); This creates an empty stack.
Methods In Stack class
There are only five methods in Stack class. These are
- push(): This method takes an object as argument which is inserted in stack at top location. This method returns the same object in the argument.
- pop(); This method deletes and returns object at the top position in stack. When we try to call pop method in empty stack, jvm throws EmptyStackException.
- peek(); this method take no argument and return the object at the top position without deleting it. Jvm throws EmptyStackException exception if we try to call peek method in an empty stack.
- empty(); This method checks whether the stack is empty or not and return true if stack is empty. Otherwise it will return false.
- search(); We have to pass object to be searched as argument and then this method search stack for that object. If object is found it returns the position of that object as an positive integer. If it do not find desired object it will return -1.
Lets look at following demo example to get clear idea about methods of Stack in Java collection.
package stack.methods; import java.util.EmptyStackException; import java.util.Stack; public class MainClassStack { public static void main(String[] args) { Stack s=new Stack(); try{ s.pop(); //this will cause Exception because stack is empty }catch(EmptyStackException e){ e.printStackTrace(); } System.out.println(s.push("Log"));// Push method returns object that is pushed in //stack s.push("Raj"); System.out.println(s.peek());// Thes will return object at the top of stack System.out.println(s.search("Log")); // this method will return the position of String object "Log"; System.out.println(s.search("Pyarb"));// Here return -1 because object specified //is not present in the stack s.pop(); System.out.println(s.pop());// pop method will return the object that is returned try{ s.peek();//Exception is arised becase stack is empty at this point of time }catch(EmptyStackException e){ e.printStackTrace(); } } }
Output of this program will be like this.
java.util.EmptyStackException at java.util.Stack.peek(Stack.java:102) at java.util.Stack.pop(Stack.java:84) at stack.methods.MainClassStack.main(MainClassStack.java:11) Log Raj 2 -1 Log java.util.EmptyStackException at java.util.Stack.peek(Stack.java:102) at stack.methods.MainClassStack.main(MainClassStack.java:28)
I hope every thing is clear in this example where a stack s is created and two string objects are pushed and poped. The only thing that we have to understand is bit about search method.
Search method counts downward from the top of stack start from 1. So when searching object “Log” in above example we get output 2.
Comment down below if you have any query.