English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about the Java Stack class and its methods through examples.
The Java Collection Framework has a class named Stack, which provides the functionality of the stack data structure.
This Stack class inherits from the Vector class.
In the stack, elements areLast In First Outin the way of storage and access. That is, elements are added to the top of the stack and removed from the top of the stack.
To create a stack, we must first import the java.util.Stack package. After importing the package, we can use Java to create a stack.
Stack<Type> stacks = new Stack<>();
Here, Type indicates the type of the stack. For example,
//Create an integer type stack Stack<Integer> stacks = new Stack<>(); //Create a string type stack Stack<String> stacks = new Stack<>();
Since Stack inherits from the Vector class, it inherits all methods of Vector. To learn about different Vector methods, please visitJava Vector Class.
In addition to these methods, the Stack class includes5methods that distinguish it from Vector.
To add an element to the top of the stack, we use the push() method. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //Add elements to the Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: "); + animals); } }
Output Result
Stack: [Dog, Horse, Cat]
To delete an element from the top of the stack, we use the pop() method. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //To add an element to the Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Initial stack: "); + animals); //Delete stack element, following the Last In, First Out (LIFO) principle String element = animals.pop(); System.out.println("Delete element: "); + element); } }
Output Result
Initial stack: [Dog, Horse, Cat] Delete element: Cat
The peek() method returns an object from the top of the stack. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //Add elements to the Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: "); + animals); //Access element from the top, following the Last In, First Out (LIFO) principle String element = animals.peek(); System.out.println("Top element: "); + element); } }
Output Result
Stack: [Dog, Horse, Cat] Top element: Cat
To search for an element in the stack, we use the search() method. It returns the position of the element from the top of the stack. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //Add elements to the Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: "); + animals); //Search for element int position = animals.search("Horse"); System.out.println("The position of element Horse: "); + position); } }
Output Result
Stack: [Dog, Horse, Cat] The position of element Horse: 2
To check if the stack is empty, we use the empty() method. For example,
import java.util.Stack; class Main { public static void main(String[] args) { Stack<String> animals = new Stack<>(); //Add elements to the Stack animals.push("Dog"); animals.push("Horse"); animals.push("Cat"); System.out.println("Stack: "); + animals); //Check if the stack is empty boolean result = animals.empty(); System.out.println("Is the stack empty? "); + result); } }
Output Result
Stack: [Dog, Horse, Cat] Is the stack empty? false
This Stack class provides a direct implementation of the stack data structure. However, it is recommended not to use it. Instead, use the ArrayDeque class (which implements the Deque interface) to implement the stack data structure in Java.
For more information, please visit:Java ArrayDeque