Java Stack class

The java.util.Stack class implements the standard Stack data structure. The class is based on the basic principle of last-in-first-out (LIFO).

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 only defines the default constructor, which creates an empty stack object:

Stack()

 

Stack Methods

Stack includes all of the methods defined by Vector, and adds some new methods of its own. −

Sl. No.Method and Description
1

boolean empty()

Tests if this stack is empty. Returns true if the stack is empty, and returns false if the stack contains elements.

2

Object peek( )

Returns the element on the top of the stack, but does not remove it.

3

Object pop( )

Returns the element on the top of the stack, removing it in the process.

4

Object push (Object element)

Pushes the element onto the stack. Element is also returned.

5

int search (Object element)

Searches for element in the stack. If found, its offset from the top of the stack is returned. Otherwise, -1 is returned.

 

Example

The following program illustrates different methods supported by the Stack class

StackDemo.java

import java.util.Iterator;
import java.util.Stack;

public class StackDemo {

public static void main(String[] args) {
// Create Stack object
Stack stack = new Stack<>();
Iterator iterator;

// Push operation
custom_push(stack);

// Iterate through the elements of stack
iterator = stack.iterator();
System.out.println("\nStack elements: ");
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();

// Pop operation
custom_pop(stack);

// Check empty or not
System.out.println(custom_empty(stack));

// Push operation
custom_push(stack);

// Iterate through the elements of stack
iterator = stack.iterator();
System.out.println("\nStack elements: ");
while (iterator.hasNext()) {
System.out.print(iterator.next() + " ");
}
System.out.println();

// Peek operation
custom_peek(stack);

// Search operations
custom_search(stack, 4);
custom_search(stack, 7);

// Check empty or not
System.out.println(custom_empty(stack));
}

// Pushing element onto the top of the stack
static void custom_push(Stack st) {
System.out.println("\nPush operation:~ ");
for (int i = 1; i <= 5; i++) {
System.out.println("Item pushed: " + i);
st.push(i);
}
}

// Popping element from the top of the stack
static void custom_pop(Stack st) {
System.out.println("\nPop operation:~ ");

for (int i = 1; i <= 5; i++) {
Integer item = (Integer) st.pop();
System.out.println("Item popped: "+ item);
}
}

// Displaying element on the top of the stack
static void custom_peek(Stack st) {
System.out.println("\nPeek operation:~ ");
Integer element = (Integer) st.peek();
System.out.println("Element on stack top : " + element);
}

// Searching element in the stack
static void custom_search(Stack st, int element) {
System.out.println("\nSearch operation:~ ");
Integer pos = (Integer) st.search(element);

if (pos == -1) {
System.out.println("Element " + element + " is not found");
} else {
System.out.println("Element " + element + " is found at position " + pos);
}
}

// Checking stack is empty or not
static String custom_empty(Stack st) {
System.out.println("\nChecking stack is empty or not:~ ");
if(st.empty() == true)
return "Stack is empty!";
else
return "Stack is not empty!";
}

}

Output:

Push operation:~ 
Item pushed: 1
Item pushed: 2
Item pushed: 3
Item pushed: 4
Item pushed: 5

Stack elements:
1 2 3 4 5

Search operation:~
Element 2 is found at position 4

Pop operation:~
Item popped: 5
Item popped: 4
Item popped: 3
Item popped: 2
Item popped: 1

Checking stack is empty or not:~
Stack is empty!

Push operation:~
Item pushed: 1
Item pushed: 2
Item pushed: 3
Item pushed: 4
Item pushed: 5

Stack elements:
1 2 3 4 5

Peek operation:~
Element on stack top : 5

Search operation:~
Element 4 is found at position 2

Search operation:~
Element 7 is not found

Checking stack is empty or not:~
Stack is not empty!

 

Use ArrayDeque In place of Stack

The Stack class provides the direct implementation of the stack data structure. However, it is recommended not to use it. Instead, better use the ArrayDeque class (implements the Deque interface) to implement the Stack data structure in Java.

For example:

Deque<Integer> stack = new ArrayDeque<Integer>();