in , ,

Java Stack Class

Java Stack

Java Stack Class

In this tutorial, we will find out about the Java Stack class and its strategies with the help of examples.

The Java assortments system has a class named Stack that gives the functionality of the stack data structure.

The Stack class broadens the Vector class.


Stack Implementation

In stack, elements are stored and accessed in Last In First Out way. That is, components are added to the highest point of the stack and eliminated from the highest point of the stack.


Making a Stack

So as to make a stack, we should import the java.util.Stack package first. When we import the package, here is the means by which we can make a stack in Java.

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

Here, Type indicates the stack’s type. For example,

// Create Integer type stack
Stack<Integer> stacks = new Stack<>();

// Create String type stack
Stack<String> stacks = new Stack<>();

Stack Methods

Since Stack extends the Vector class, it inherits all the methods Vector. To learn about different Vector methods, visit Java Vector Class.

Besides these methods, the Stack class includes 5 more methods that distinguish it from Vector.


push() Method

To add an element to the top of the stack, we use the push() method. For example,

import java.util.Stack;

class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();

        // Add elements to Stack
        animals.push("Dog");
        animals.push("Horse");
        animals.push("Cat");

        System.out.println("Stack: " + animals);
    }
}

Output

Stack: [Dog, Horse, Cat]

pop() Method

To remove an element from the top of the stack, we use the pop() method. For example,

import java.util.Stack;

class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();

        // Add elements to Stack
        animals.push("Dog");
        animals.push("Horse");
        animals.push("Cat");
        System.out.println("Initial Stack: " + animals);

        // Remove element stacks
        String element = animals.pop();
        System.out.println("Removed Element: " + element);
    }
}

Output

Initial Stack: [Dog, Horse, Cat]
Removed Element: Cat

peek() Method

The peek() method returns an object from the top of the stack. For example,

import java.util.Stack;

class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();

        // Add elements to Stack
        animals.push("Dog");
        animals.push("Horse");
        animals.push("Cat");
        System.out.println("Stack: " + animals);

        // Access element from the top
        String element = animals.peek();
        System.out.println("Element at top: " + element);

    }
}

Output

Stack: [Dog, Horse, Cat]
Element at top: Cat

search() Method

To look through a component in the stack, we use the search() strategy. It returns the position of the component from the highest point of the stack. For instance,

import java.util.Stack;

class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();

        // Add elements to Stack
        animals.push("Dog");
        animals.push("Horse");
        animals.push("Cat");
        System.out.println("Stack: " + animals);

        // Search an element
        int position = animals.search("Horse");
        System.out.println("Position of Horse: " + position);
    }
}

Output

Stack: [Dog, Horse, Cat]
Position of Horse: 2

empty() Method

To check whether a stack is empty or not, we use the empty() method. For example,

import java.util.Stack;

class Main {
    public static void main(String[] args) {
        Stack<String> animals= new Stack<>();

        // Add elements to Stack
        animals.push("Dog");
        animals.push("Horse");
        animals.push("Cat");
        System.out.println("Stack: " + animals);

        // Check if stack is empty
        boolean result = animals.empty();
        System.out.println("Is the stack empty? " + result);
    }
}

Output

Stack: [Dog, Horse, Cat]
Is the stack empty? false

Use ArrayDeque Instead of Stack

The Stack class gives the immediate execution of the stack data structure. Be that as it may, it is prescribed not to use it. Instead, use the ArrayDeque class (implements the Deque interface) to implement the stack data structure in Java.

Java ArrayDeque

Why use Deque over Stack?


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Vector

Java Vector

Java Queue Interface

Java Queue Interface