Tech and Media Labs
This site uses cookies to improve the user experience.


Java Stack

Jakob Jenkov
Last update: 2018-09-21

The Java Stack class, java.util.Stack, is a classical stack data structure. You can push elements to the top of a Stack and pop them again, meaning read and remove the elements from the top of the stack.

The Java Stack class actually implements the Java List interface, but you rarely use a Stack as a List - except perhaps if you need to inspect all elements currently stored on the stack.

Stack Basics

A Stack is a data structure where you add elements to the "top" of the stack, and also remove elements from the top again. This is also referred to as the "Last In First Out (LIFO)" principle. In contrast, a Java Queue uses a "First In First Out (FIFO)" principle, where elements are added to the end of the queue, and removed from the beginning of the queue.

Create a Stack

To use a Java Stack you must first create an instance of the Stack class. Here is an example of creating a Java Stack instance:

Stack stack = new Stack();

Push Element on Stack

Once you have a Java Stack instance, you can push elements to the top of the Stack. The elements you push onto the Stack must be Java objects. Thus, you actually push objects to the Stack.

You push elements onto a Java Stack using its push() method. Here is an example of pushing an element (object) onto a Java Stack:

Stack stack = new Stack();

stack.push("1");

This Java example pushes a Java String with the text 1 onto the Stack. The String 1 is then stored at the top of the Stack.

Pop Element From Stack

Once an element has been pushed onto a Java Stack, you can pop that element from the Stack again. Once an element is popped off the Stack, the element is removed from the Stack. The top element of the Stack is then whatever element that was pushed onto the Stack just before the element just popped.

You pop an element off a Java Stack using the pop() method. Here is an example of popping an element off a Stack using the pop() method:

Stack stack = new Stack();

stack.push("1");

String topElement = stack.pop();

Once an element is popped off a Stack, the element is no longer present on the Stack.

Peek at Top Element of Stack

The Java Stack class has a method called peek() which enables you to see what the top element on the Stack is, without popping off the element. Here is an example of peeking at the top of a Java Stack:

Stack stack = new Stack();

stack.push("1");

String topElement = stack.peek();

After running this Java example the topElement variable will contain the String object 1 which was pushed onto the Stack just before peek() was called. The String object is still present on the Stack after calling peek().

Search the Stack

You can search for an object on the stack to get it's index, using the search() method. The object's equals() method is called on every object on the Stack to determine if the searched-for object is present on the Stack. The index you get is the index from the top of the Stack, meaning the top element on the Stack has index 1.

Here is how you search a Stack for an object:

Stack stack = new Stack();

stack.push("1");
stack.push("2");
stack.push("3");

int index = stack.search("3");     //index = 1

Stack Use Cases

Stack's are really handy for some types of data processing, for instance if you are parsing an XML file using either SAX or StAX. For an example, see my Java SAX Example in my Java XML tutorial.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC