Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Collection Framework in Java?
3.
Stack Class in Java
4.
The constructor of Stack Class
5.
Creating a Stack in Java
6.
Methods of Stack Class
6.1.
Stack Class push() Method
6.2.
Stack Class pop() Method
6.3.
Stack Class peek() Method
6.4.
Stack Class empty() Method
6.5.
Stack Class search() Method
7.
Implementation
7.1.
Java
7.1.1.
Output
7.2.
Time Complexity of Operations
8.
Prioritize the use of  Deque over Stack
8.1.
Why Use Deque over Stack?
9.
Legacy Classes in Java
10.
Frequently Asked Questions
10.1.
What is stack in collection in Java? 
10.2.
When to use stack in Java? 
10.3.
How to take stack in Java? 
10.4.
What is the stack size in Java collection? 
11.
Conclusion
Last Updated: Jul 9, 2024
Easy

Stack Class in Java

Introduction

Data structures are the basic need of every program. It's time-consuming to create a data structure from scratch every time. For this purpose, programming languages provide built-in data structures. Standard Template Library(STL) in C++, and Collection Framework in Java, serve this purpose.

The earlier versions of Java didn't have a Collection Framework. There were classes to store objects. Those classes are now known as Legacy Classes. Some examples of legacy classes are Vector, Dictionary, HashTable, and Stack etc.

Stacks

When collections came, they supported generics. So, legacy classes were restructured and re-engineered to make them compatible with Collections. Stack is one of the legacy classes of Java.

 

In this article, we'll be discussing the Stack in Java.

So, let’s get started! 

Stack

What is Collection Framework in Java?

A single unit of objects in Java is referred to as a collection. A collection is a framework that allows us to store and modify a group of objects. Java Collections can perform all data operations such as searching, sorting, insertion, manipulation, and deletion.
 

JDK 1.2 introduced the collection framework. It is present in the Java.util.Collection package. It contains three sections as given below:

  • Interfaces: Interfaces are designed to allow the manipulation of collections independently. The Java hierarchy consists of six core collection interfaces. They are Collection, Set, List, SortedSet, Map and SortedMap.
  • Implementations: It contains the implementations of the collection interfaces. They are blueprints of data structures. For example, stack linked lists.
  • Algorithms: Algorithms contain functions designed to make collections easy to use and implement. For example, searching, sorting, insertion, deletion etc.

Stack Class in Java

The Stack class is based on the principle of Last in First out (LIFO). The element inserted last will be the first to be removed. The Stack class provided by Java Collections implements five interfaces. The list of those interfaces is given below:

  1. Serializable: This interface allows classes to be serialized and deserialized.
  2. Cloneable: This interface allows a class to clone its objects.
  3. RandomAccess: This interface is used by sequential or List implementations to support fast random access(constant time).
  4. Iterable<E>: This interface represents a collection of objects which can be iterated.
  5. Collection<E>: This is the root interface in the collection hierarchy. It represents a group of objects, known as its elements.
  6. List<E>: It is a child of the Collection interface. It provides a way to store the ordered elements. 

The constructor of Stack Class

The Stack class in Java contains only one constructor. It creates an empty Stack.

Constructor: Stack()

Creating a Stack in Java

A Stack can be created in Java using the above constructor.

Syntax: 

Stack<Type> name_of_stack = new Stack<Type>();

Here, type specifies the type of object such as Integer, String and Float etc.

Example 

Stack<Integer> stack1 = new Stack<Integer>();
Stack<String> stack2 = new Stack<String>();
//stack1, stack2 represents name of the stack created

Note: Stack can be initialized without any type as well. Such classes can store any object.

Stack stack1 = new Stack();
//stack1 represents name of the stack created

Methods of Stack Class

This built-in Stack in Java also contains predefined methods. Some of the important methods are given below:

  1. push(datatype element): This method pushes the element onto the top of the stack.
  2. pop(): This method removes the element at the top of the stack. It returns that element as the value.
  3. peek(): It returns the object at the top of the stack without removing it.
  4. empty(): It checks if the stack is empty.
  5. search(Object obj): This function returns the 1-based position where an object is on the stack.

 

Note: Stack class extends Vector class. Stack class includes all the methods defined by the Vector class and adds several of its own.

Some of the important methods inherited from the class java.util.Vector are:

Methods of Stack Class

Image credits: Java: The Complete Reference By Herbert Schildt

Stack Class push() Method

The push() method is used to add an item to the top of the stack. This method takes a single argument, which is the item to be pushed onto the stack. After the call, the new item becomes the top element of the stack.

Example:

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(10);
        stack.push(20);
        stack.push(30);
        System.out.println("Stack after pushes: " + stack);  // Output: [10, 20, 30]
    }
}

Stack Class pop() Method

The pop() method is used to remove the item at the top of the stack and return it. This method modifies the stack by removing the top element. If the stack is empty when pop() is called, it throws an EmptyStackException.

Example:

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(10);
        stack.push(20);
        stack.push(30);
        int topElement = stack.pop();
        System.out.println("Popped element: " + topElement);  // Output: 30
        System.out.println("Stack after pop: " + stack);  // Output: [10, 20]
    }
}

Stack Class peek() Method

The peek() method returns the item at the top of the stack without removing it. This method provides a way to look at the top element without modifying the stack. If the stack is empty when peek() is called, it throws an EmptyStackException.

Example:

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(10);
        stack.push(20);
        stack.push(30);
        int topElement = stack.peek();
        System.out.println("Top element: " + topElement);  // Output: 30
        System.out.println("Stack after peek: " + stack);  // Output: [10, 20, 30]
    }
}

Stack Class empty() Method

The empty() method checks whether the stack is empty or not. It returns true if the stack contains no elements, and false otherwise.

Example:

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        System.out.println("Is stack empty? " + stack.empty());  // Output: true
        stack.push(10);
        System.out.println("Is stack empty? " + stack.empty());  // Output: false
    }
}

Stack Class search() Method

The search() method searches for an item in the stack and returns the 1-based position from the top of the stack. If the item is found, it returns the distance from the top of the stack. If the item is not found, it returns -1.

Example:

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(10);
        stack.push(20);
        stack.push(30);
        int position = stack.search(20);
        System.out.println("Position of 20: " + position);  // Output: 2
        position = stack.search(40);
        System.out.println("Position of 40: " + position);  // Output: -1
    }
}

 

Now, let’s see the implementation of Stack in Java.

Implementation

  • Java

Java

import java.util.*;

public class DemoStack {
static void showpush(Stack<Integer> st, int a) {
st.push(a);
System.out.println("push(" + a + ")");
System.out.println("stack: " + st);
}

static void showpop(Stack<Integer> st) {
System.out.print("pop -> ");
Integer a = st.pop();
System.out.println(a);
System.out.println("stack: " + st);
}

public static void main(String args[]) {
Stack<Integer> st = new Stack<Integer>();
System.out.println("stack: " + st);
showpush(st, 42);
showpush(st, 66);
showpush(st, 99);
showpop(st);
showpop(st);
showpop(st);
try {
showpop(st);
} catch (EmptyStackException e) {
System.out.println("empty stack");
}
}
}

Output

stack: []
push(42)
stack: [42]
push(66)
stack: [42, 66]
push(99)
stack: [42, 66, 99]
pop -> 99
stack: [42, 66]
pop -> 66
stack: [42]
pop -> 42
stack: []
pop -> empty stack

Run your Java Code here

Must Read Stack Operations

Time Complexity of Operations

The time complexity of push(), pop(), peek(), isEmpty(), isFull() and size() is constant, i.e., O(1).

Check out this video for more concepts about Stack data structure.

Prioritize the use of  Deque over Stack

In Java, while the Stack class has traditionally been used for stack operations, it's generally recommended to use the Deque interface (short for "double-ended queue") over Stack for several reasons. Here’s an explanation of why Deque is preferred and how it can be used for stack operations:

Why Use Deque over Stack?

1. Inheritance and Design:

  • The Stack class extends Vector, which is synchronized and thread-safe. This can lead to unnecessary overhead if thread safety is not required.
  • Deque is a more general-purpose interface that is designed for both stack (LIFO) and queue (FIFO) operations, providing more flexibility.

2. API and Functionality:

  • Deque provides a richer set of methods for stack operations compared to Stack. Methods like addFirst(), removeFirst(), peekFirst(), etc., can be used to implement stack-like behavior.
  • Deque also supports operations at both ends, which Stack does not, making Deque more versatile.

3. Performance:

  • The implementations of Deque (like ArrayDeque and LinkedList) are generally more efficient than Stack for stack operations because they do not have the synchronization overhead of Vector.

4. Best Practices:

  • The Java Collections Framework has evolved, and modern best practices favor the use of interfaces over concrete classes. Using Deque aligns with this practice and provides better abstraction.

Legacy Classes in Java

As we’ve seen above, legacy classes were included in Java from the initial versions. But when Collections were introduced, they were on the verge of depreciation. To make them compatible, Java restructured those classes. They are not obsolete, but their better alternatives are present in Java. 

For example, the Deque interface and its implementations provide a more comprehensive and consistent set of LIFO stack operations. Therefore they should be used instead of Stack class.

 

Legacy classes in Java are synchronized. The Java.util package contains the following legacy classes: HashTableStackDictionaryProperties, and Vector.

 

To learn in more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples, you can refer to the official documentation of Java here.

 

Now, let’s see some of the frequently asked questions.

Frequently Asked Questions

What is stack in collection in Java? 

A stack in Java is a data structure that follows the Last-In-First-Out (LIFO) principle for adding and removing elements.

When to use stack in Java? 

Use a stack in Java for algorithms requiring LIFO behavior, such as depth-first search, expression evaluation, and backtracking.

How to take stack in Java? 

Use java.util.Stack class or prefer Deque implementations like ArrayDeque for stack operations, offering push, pop, and peek methods.

What is the stack size in Java collection? 

The size of a stack in Java collections is dynamically managed, limited only by available memory.

Conclusion

In this article, we’ve learned about Stack in Java. Java Collection Framework provides several other classes like Linked list, vector, Set and Hashmap etc.

If you want to learn more about Java, here’s a fantastic course to learn Java along with Data structure and Algorithms offered by Coding Ninjas. Do check it out.

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Code360.

You can also consider our Mern Stack Course to give your career an edge over others.

Happy Learning!

Live masterclass