Stack class in Java
Stack is a linear data structure which follows the Last-In-First-Out (LIFO) approach, where the last element inserted is the first one to be removed.
In this post, we’ll see how to use the Stack class in Java to create a stack. Java provides the Stack class as part of the Java Collections Framework. For an overview of the Java Collections Framework, check out my post Overview of the Java Collections Framework.
Methods provided by the Stack class are:
- push(E element)
- pop()
- peek()
- empty()
- search()
Usually, a stack doesn’t allow searching through its elements without popping them out. But the search()
method allows that, and exposes all the elements of the stack. For this reason and others, Stack class isn’t preferred. Instead, implementations of the Deque interface such as ArrayDeque or LinkedList are typically used.
Stack class allows null
elements, but it is usually not recommended to insert nulls.
The following example illustrates how to use the Stack class in Java.
Example
Output
Now, let’s look more closely at the methods provided by the Stack class.
Methods
1) push(E element)
This method pushes an element onto the top of the stack.
Consider a stack with the following elements from top: “Banana” “Apple”
This method pushes the string “Orange” to the top of the stack. Now, the elements in the stack are: “Orange” “Banana” “Apple”
2) pop()
This method removes and returns the element at the top of the stack. If the stack is empty, an ‘EmptyStackException’
exception is thrown.
This method removes and returns the string “Orange” which is at the top of the stack.
3) peek()
It returns the element at the top of the stack, but doesn’t remove it. As the name indicates, it is similar to peeking at the top of the stack.
This call to the method returns the string “Banana” which is at the top of the stack, but doesn’t remove it from the stack.
4) empty()
This method tests if the stack is empty. It returns true if the stack is empty or false otherwise.
Current elements in the stack: “Banana” “Apple”
In this case, the call to the method returns false, because the stack is not empty.
5) search(E element)
This method searches the stack for the given element. If the elemsent is found, it returns the position of the element from the top of the stack, otherwise it returns -1.
This call to the method returns the value 2, because the string “Apple” is at second position from the top of the stack.
Now since you know how to use the Stack class in Java, check out other ways to create a stack: