Java – Stack class

In this article, we will discuss Stack class in detail

Key points about Stack:

  • Stack is a legacy class
  • introduced in Java 1.0 version
  • works in Last-In First-Out order (LIFO order)
  • most of its methods are synchronized i.e.; thread-safe

1. Stack:

  • Stack is a sub-class of Vector (i.e.; Stack extends Vector)
  • All properties are same as that of Vector class
  • Its data structure is designed in such a way that elements added last to the Stack will be returned first
  • That is, it follows Last-In First-Out order
  • Present in java.util package and extends java.util.Vector class

2. Stack constructors:

Stack s = new Stack();

  • Stack defines default constructor which creates an empty Stack object

3. Stack methods:

  • Stack specific methods
 Stack methodsDescription
Object push(Object obj);add/inserts new element/object into stack
Object pop();removes and return top of the stack
Object peek();returns top of the stack (just returns but doesn’t remove unlike pop() operation
boolean empty();check whether invoking stack is empty or not
returns true, if stack is empty; otherwise, false
int search(Object obj)searches specified element/object from invoking stack

 

returns offset from top of the stack, if element is available; otherwise returns -1, if element not available

4. Stack examples:

StackPushAndPop.java

package in.bench.resources.java.collection;

import java.util.Stack;

public class StackPushAndPop {

	public static void main(String[] args) {

		// creating Stack object of type String
		Stack<String> stk = new Stack<String>();

		// adding elements to Stack object
		stk.push("Sundar Pichai");
		stk.push("Satya Nadella");
		stk.push("Shiv Nadar");
		stk.push("Shantanu Narayen");
		stk.push("Francisco D’Souza");

		System.out.println("Iterating Stack values\n");

		// Iterating using enhanced for-loop
		for(String str : stk){
			System.out.println(str);
		}

		// peeking top of the stack - doesn't remove
		String peekStr = stk.peek();
		System.out.println("\n\nElement peeked at top "
				+ "of the Stack : " + peekStr);

		// removing top element of the Stack
		String poppedStr = stk.pop();
		System.out.println("\n\nElement removed at top "
				+ "of the Stack : " + poppedStr);

		// check whether Stack is empty
		boolean isStackEmpty = stk.empty();
		System.out.println("\n\nWhether Stack is Empty : "
				+ isStackEmpty);

		// to print all values of Stack
		System.out.println("\n\nStack values after removing "
				+ "top of the Stack \n" + stk);
	}
}

Output:

Iterating Stack values

Sundar Pichai
Satya Nadella
Shiv Nadar
Shantanu Narayen
Francisco D’Souza

Element peeked at top of the Stack : Francisco D’Souza

Element removed at top of the Stack : Francisco D’Souza

Whether Stack is Empty : false

Stack values after removing top of the Stack
[Sundar Pichai, Satya Nadella, Shiv Nadar, Shantanu Narayen]

Note: All methods of Stack are synchronized hence it is thread-safe

References:

Happy Coding !!
Happy Learning !!

Java - Enumeration interface with example
Java - ArrayList v/s Vector