Stack Class in Java Example

  • Stack class in java is one of the collection interface class which is subclass of Vector class.
  • Stake class supports usual push and pop operations.
  • In contrast to queue, Stack class has last-in first-out(LIFO) data structure. So item which is inserted at top will be available first.
  • Stack class extends Vector class of List interface so it includes all methods of vector class and also it has it's own several methods to perform push and pop operations like looking top item from stack, removing top item from stack, push new item at top of stack, searching item from stack and check whether stack is empty.
java stack class example



Important Methods of Stack Class in Java


  • boolean empty() : This method of stack class will help you to check if stack is empty.
  • Object peek( ) : It will look and return top element of stack. It will not remove element from stack.
  • Object pop( ) : It will remove item from top of the stack and return the value.
  • Object push(E item) : It will push an item on top of the stack
  • int search(Object o) : It will determine if object is exist in stack. If exist then it will return item's index. Top item of stack has index = 1.
Below given practical example of java stack class will show you how above methods work.

package JAVAExamples;

import java.util.Stack;

public class JavaStackExample {
 public static void main(String args[]) {
  //Create stc stack.
  Stack stc = new Stack();
  //Initially stack will be empty.
  System.out.println("Stack is empty? : "+stc.empty());
  //Push items in stack.
  stc.push("Item1");
  stc.push("Item2");
  stc.push("Item3");
  //Now stack if filled.
  System.out.println("Now stack empty? : "+stc.empty());
  //Print stack items.
  System.out.println("stack Ietms : " + stc);
  //Get top item from stack.
  System.out.println("Top item in stack is : "+stc.peek());
  //Print stack items after peek.
  System.out.println("stack Ietms after peek : " + stc);
  //Get and remove top item from stack.
  System.out.println("Get top item from stack and removed it from stack : "+stc.pop());
  //Print stack items after pop.
  System.out.println("stack Ietms after pop : " + stc);
  //Push new item in stack.
  stc.push("Item4");
  //Print stack after inserting new item.
  System.out.println("Now stack Ietms are : " + stc);
  //Search item from stack which is available.
  System.out.println("Search Item1 in stack is at index : "+stc.search("Item1"));
  //Search item from stack which is not available in stack.
  System.out.println("Search Item7 in stack which is not available is : "+stc.search("Item7"));
 }
}


Output :
Stack is empty? : true
Now stack empty? : false
stack Ietms : [Item1, Item2, Item3]
Top item in stack is : Item3
stack Ietms after peek : [Item1, Item2, Item3]
Get top item from stack and removed it from stack : Item3
stack Ietms after pop : [Item1, Item2]
Now stack Ietms are : [Item1, Item2, Item4]
Search Item1 in stack is at index : 3
Search Item7 in stack which is not available is : -1

Now i think you will understand how and when to use stack class in java.

No comments:

Post a Comment