Java Stack Class Example


In this article, we will discuss important methods or APIs of the Java Stack class from the java.util package. We also discuss how to convert Array to Stack, Stack to List and vice versa.


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.

All Stack Class Methods with Example

Let's demonstrates the usage of all Stack class methods with examples.
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 StackClassMethods {

    public static void main(String[] args) {
        pushMethod();
        searchMethod();
        popMethod();
        peekMethod();
        emptyMethod();
    }

    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"));
    }

    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);
    }

    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);
    }

    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());
    }

    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:
Elements in the stack: [Java, JEE, C, C++, Spring, Hibernate]
Searching 'Spring' in stack: 2
Removed object is: Hibernate
Elements after remove: [Java, JEE, C, C++, Spring]
Top object is: Hibernate
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

Comments