Java Stack Class Methods with Examples

In this article, we will discuss important methods or APIs of the Java Stack class from the java.util package.

What is a Stack?

A stack is an ordered list in which insertion and deletion are done at one end, called a top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First out (LIFO) or First in Last out (FILO) list.
From Javadoc: The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.

Stack Concepts

  • When an element is inserted in a stack, the concept is called a push.
  • When an element is removed from the stack, the concept is called pop.
  • Trying to pop out an empty stack is called underflow (treat as Exception).
  • Trying to push an element in a full stack is called overflow (treat as Exception).

Stack Class Methods Summary

  • boolean empty() - This method is used to test if this stack is empty.
  • E peek() - This method looks at the object at the top of this stack without removing it from the stack.
  • E pop() - This method is used to remove the object at the top of this stack and returns that object as the value of this function.
  • E push(E item) - This method pushes an item onto the top of this stack.
  • int search(Object o) - This method returns the 1-based position where an object is on this stack.

Stack Class Diagram

The below class diagram shows a list of APIs that Stack class provides.

Stack Class Methods with Example

Let's demonstrates the usage of all Stack class methods with examples.

push(String item)

This method pushes an item onto the top of this stack.
Let's demonstrate the usage of push() method with an example:
    private static void pushMethod() {
        // creating stack
        Stack < String > stack = new Stack < > ();

        // populating stack
        stack.push("Java");
        stack.push("JEE");
        stack.push("C");
        stack.push("C++");
        stack.push("Spring");
        stack.push("Hibernate");

        // checking elements
        System.out.println("Elements in the stack: " + stack);
    }
Output:
Elements in the stack: [Java, JEE, C, C++, Spring, Hibernate]

E pop()

This method is used to remove the object at the top of this stack and returns that object as the value of this function.
Let's demonstrate the usage of pop() method with an example:
    private static void popMethod() {
        // creating stack
        Stack < String > stack = new Stack < > ();

        // populating stack
        stack.push("Java");
        stack.push("JEE");
        stack.push("C");
        stack.push("C++");
        stack.push("Spring");
        stack.push("Hibernate");

        // removing top object
        System.out.println("Removed object is: " + stack.pop());

        // elements after remove
        System.out.println("Elements after remove: " + stack);
    }
Output:
Removed object is: Hibernate
Elements after remove: [Java, JEE, C, C++, Spring]

int search(Object o)

This method returns the 1-based position where an object is on this stack.
Let's demonstrate the usage of search() method with an example:
    private static void searchMethod() {
        // creating stack
        Stack < String > stack = new Stack < > ();

        // populating stack
        stack.push("Java");
        stack.push("JEE");
        stack.push("C");
        stack.push("C++");
        stack.push("Spring");
        stack.push("Hibernate");

        // searching 'Spring' element
        System.out.println("Searching 'Spring' in stack: " + stack.search("Spring"));
    }
Output:
Searching 'Spring' in stack: 2

E peek()

This method looks at the object at the top of this stack without removing it from the stack.
Let's demonstrate the usage of peek() method with an example:
    private static void peekMethod() {
        // creating stack
        Stack < String > stack = new Stack < > ();

        // populating stack
        stack.push("Java");
        stack.push("JEE");
        stack.push("C");
        stack.push("C++");
        stack.push("Spring");
        stack.push("Hibernate");

        // checking the top object
        System.out.println("Top object is: " + stack.peek());
    }
Output:
Top object is: Hibernate

boolean empty()

This method is used to test if this stack is empty.
Let's demonstrate the usage of empty() method with an example:
    private static void emptyMethod() {
        // creating stack
        Stack < String > stack = new Stack < > ();

        // populating stack
        stack.push("Java");
        stack.push("JEE");
        stack.push("C");
        stack.push("C++");
        stack.push("Spring");
        stack.push("Hibernate");

        // checking stack
        System.out.println("Is stack empty: " + stack.empty());
    }
Output:
Is stack empty: false

Java Array to Stack Example

Let us explore on “How to create a Stack object with a given String array” here.
package com.javaguides.corejava.api.util;

import java.util.Stack;

/**
 * Class demonstrates the usage of Stack class methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class ArrayToStackExample {

    public static void main(String[] args) {
        convertArrayToStack();
    }

    private static void convertArrayToStack() {
        String[] strArr = {
            "Java",
            "JEE",
            "C",
            "C++",
            "Spring",
            "Hibernate"
        };
        Stack < String > stack = new Stack < > ();
        for (String string: strArr) {
            stack.push(string);
        }
        System.out.println("Non-Empty stack : " + stack);
    }
}
Output:
Non-Empty stack : [Java, JEE, C, C++, Spring, Hibernate]

Java List to Stack Example

Let us explore on “How to create a Stack object with a given List of strings” here.
package com.javaguides.corejava.api.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Class demonstrates the usage of Stack class methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class ListToStackExample {

 public static void main(String[] args) {
  convertListToStack();
 }

 private static void convertListToStack() {
  Stack<String> stack = new Stack<>();
  List<String> list = new ArrayList<>();
  list.add("Java");
  list.add("JEE");
  list.add("C");
  list.add("C++");
  list.add("Spring");
  list.add("Hibernate");
  System.out.println("Non-Empty stack addAll Operation : " + stack.addAll(list));
  System.out.println("Non-Empty stack : " + stack);
 }
}
Output:
Non-Empty stack addAll Operation : true
Non-Empty stack : [Java, JEE, C, C++, Spring, Hibernate]

Java Stack to List Example

Let us explore on “How to create a List object with a Stack of strings” here.
package com.javaguides.corejava.api.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Class demonstrates the usage of Stack class methods with examples
 * 
 * @author Ramesh Fadatare
 *
 */
public class StackToListExample {

 public static void main(String[] args) {
  convertStackToList();
 }

 private static void convertStackToList() {
  // creating stack
  Stack<String> stack = new Stack<>();

  // populating stack
  stack.push("Java");
  stack.push("JEE");
  stack.push("C");
  stack.push("C++");
  stack.push("Spring");
  stack.push("Hibernate");
  List<String> list = new ArrayList<>();
  list.addAll(stack);
  System.out.println("Non-Empty stack : " + stack);
  System.out.println("Non-Empty List : " + list);
 }
}
Output:
Non-Empty stack : [Java, JEE, C, C++, Spring, Hibernate]
Non-Empty List : [Java, JEE, C, C++, Spring, Hibernate]
Learn complete core Java at Java Tutorial | Learn Java Programming with Examples

Related Java API Guides

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments