top of page

DSA Java - Stack

In computer science, a stack is an abstract data type that represents a collection of elements with a last-in, first-out (LIFO) order of access. This means that the most recently added element is the first one to be removed. Stacks are commonly used in various algorithms and programming languages, including Java, to manage function calls, parse expressions, and solve other problems.

To implement a stack in Java, you can use the built-in java.util.Stack class or create your own custom implementation. Here's an example of creating a stack using the java.util.Stack class:

import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
// Create a stack

Stack<Integer> stack = new Stack<>();

// Push elements onto the stack

// Pop elements from the stack
int element1 = stack.pop(); // Removes and returns 30
int element2 = stack.pop(); // Removes and returns 20

// Print the popped elements
System.out.println("Popped elements: " + element1 + ", " + element2);

// Check if the stack is empty
boolean isEmpty = stack.isEmpty(); // Returns false

// Get the top element without removing it
int topElement = stack.peek(); // Returns 10

// Print the stack size
int size = stack.size(); // Returns 1

// Print the stack contents
System.out.println("Stack: " + stack);

In this example, we first create a stack using the Stack class, which is a generic class that allows us to specify the type of elements stored in the stack (in this case, integers). We can then use various methods provided by the Stack class to manipulate the stack.

  1. - push(element) adds an element to the top of the stack.

  2. - pop() removes and returns the element at the top of the stack.

  3. - isEmpty() checks if the stack is empty.

  4. - peek() returns the element at the top of the stack without removing it.

  5. - size() returns the number of elements in the stack.

The example demonstrates pushing elements onto the stack, popping elements from the stack, checking if the stack is empty, peeking at the top element, and obtaining the stack's size. Finally, it prints the popped elements and the current contents of the stack.

It's worth noting that the java.util.Stack class is a legacy class, and in most cases, it's recommended to use the java.util.Deque interface or its implementations, such as java.util.ArrayDeque, which offer better performance and more flexibility.

Related Posts

See All

Java Algorithms

Java is a popular programming language that provides a wide range of tools and libraries for implementing various algorithms. Here are some commonly used algorithms in Java: Sorting Algorithms: Bubble

Java - Data Structures

When it comes to Data Structures and Algorithms (DSA) in Java, there are several commonly used data structures that can be implemented. Here are some of the main data structures and their correspondin

DSA Java - Doubly Linked List

Doubly Linked List Basics Doubly Linked List is a variation of Linked list in which navigation is possible in both ways either forward and backward easily as compared to Single Linked List. Followin


bottom of page