How to use stack in Java?

Introduction to Java Stacks

Java stacks are a fundamental data structure in computer science that play a crucial role in various applications, including parsing, evaluating postfix expressions, and implementing recursive algorithms. In this article, we will delve into the world of Java stacks, exploring how to use them effectively.

What is a Stack?

A stack is a Last-In-First-Out (LIFO) data structure, meaning that the last element added to the stack will be the first one to be removed. This is in contrast to a queue, which follows the First-In-First-Out (FIFO) principle.

Types of Stacks

There are two primary types of stacks:

  • LIFO Stack: A stack that removes elements from the top of the stack.
  • FIFO Stack: A stack that removes elements from the bottom of the stack.

Creating a Stack in Java

To create a stack in Java, you can use the Stack class, which is a part of the Java Collections Framework. Here’s an example of how to create a stack:

import java.util.Stack;

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

// Add elements to the stack
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");

// Remove elements from the stack
System.out.println("Stack: " + stack);
System.out.println("Top element: " + stack.pop());
System.out.println("Stack: " + stack);
}
}

Using the push() and pop() Methods

The push() method adds an element to the top of the stack, while the pop() method removes an element from the top of the stack.

import java.util.Stack;

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

// Add elements to the stack
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");

// Remove elements from the stack
System.out.println("Stack: " + stack);
System.out.println("Top element: " + stack.pop());
System.out.println("Stack: " + stack);
}
}

Using the peek() Method

The peek() method returns the top element of the stack without removing it.

import java.util.Stack;

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

// Add elements to the stack
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");

// Remove elements from the stack
System.out.println("Stack: " + stack);
System.out.println("Top element: " + stack.peek());
System.out.println("Stack: " + stack);
}
}

Using the isEmpty() Method

The isEmpty() method checks if the stack is empty.

import java.util.Stack;

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

// Add elements to the stack
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");

// Check if the stack is empty
System.out.println("Is stack empty? " + stack.isEmpty());

// Remove elements from the stack
System.out.println("Stack: " + stack);
System.out.println("Top element: " + stack.pop());
System.out.println("Stack: " + stack);
}
}

Common Stack Operations

Here are some common stack operations:

  • push(element): Adds an element to the top of the stack.
  • pop(): Removes an element from the top of the stack.
  • peek(): Returns the top element of the stack without removing it.
  • isEmpty(): Checks if the stack is empty.
  • size(): Returns the number of elements in the stack.

Stack Implementation in Java

Here’s an example of a basic stack implementation in Java:

import java.util.Stack;

public class StackExample {
private Stack<String> stack = new Stack<>();

public void push(String element) {
stack.push(element);
}

public String pop() {
return stack.pop();
}

public String peek() {
return stack.peek();
}

public boolean isEmpty() {
return stack.isEmpty();
}

public int size() {
return stack.size();
}

public static void main(String[] args) {
StackExample stack = new StackExample();

// Add elements to the stack
stack.push("Apple");
stack.push("Banana");
stack.push("Cherry");

// Remove elements from the stack
System.out.println("Stack: " + stack.stack);
System.out.println("Top element: " + stack.peek());
System.out.println("Stack: " + stack.pop());
System.out.println("Stack: " + stack.stack);
}
}

Conclusion

In this article, we explored the basics of Java stacks, including their types, creating a stack in Java, using the push() and pop() methods, and common stack operations. We also implemented a basic stack in Java and demonstrated its usage. By understanding how to use stacks effectively, you can write more efficient and effective code in your Java applications.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top