Stack Vs Queue Explained in Java [In-Depth Tutorial]


JAVA

Author: Bashir Alam
Reviewer: Deepak Prasad

In the world of computer science and software development, data structures play a crucial role in organizing, processing, and managing data efficiently. Among the various data structures, stacks and queues are commonly used for a wide range of applications. While both stacks and queues are linear data structures that store elements sequentially, they differ significantly in terms of their underlying principles, access methods, and use cases. In Java, these data structures can be easily implemented using built-in classes and interfaces. This article will delve into the fundamental differences between stacks and queues in Java, providing an overview of their unique properties, access methods, and real-world applications. By understanding the distinctions between these data structures, developers can make informed decisions about which one to use when solving specific problems or implementing particular features in their Java applications.

 

What is a Stack in Java?

A stack is a data structure in Java that follows the Last In First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Think of a stack of plates - the last plate placed on top of the stack is the first one to be removed.

In Java, the stack data structure is implemented using the java.util.Stack class, which is a subclass of java.util.Vector. The java.util.Stack class provides several methods to manipulate the stack data structure.

 

Different Stack Operations in Java

The following are the basic operations that can be performed on a stack in Java:

  • push(): Adds an element to the top of the stack. This operation is also known as "pushing" an element onto the stack.
  • pop(): Removes the element from the top of the stack. This operation is also known as "popping" an element from the stack.
  • peek(): Returns the element at the top of the stack without removing it. This operation is also known as "peeking" at the top element of the stack.
  • isEmpty(): Checks if the stack is empty.

Let's take a closer look at each of these operations.

push():

The push() method in Java adds an element to the top of the stack. Here's an example:

Stack<Integer> stack = new Stack<Integer>();
stack.push(1);
stack.push(2);
stack.push(3);

In this example, we create a new Stack object and add three elements to the top of the stack using the push() method.

pop():

The pop() method in Java removes the element from the top of the stack. Here's an example:

Stack<Integer> stack = new Stack<Integer>();
stack.push(1);
stack.push(2);
stack.push(3);
int topElement = stack.pop();

In this example, we create a new Stack object and add three elements to the top of the stack using the push() method. We then use the pop() method to remove the top element from the stack and store it in the topElement variable.

peek():

The peek() method in Java returns the element at the top of the stack without removing it. Here's an example:

Stack<Integer> stack = new Stack<Integer>();
stack.push(1);
stack.push(2);
stack.push(3);
int topElement = stack.peek();

In this example, we create a new Stack object and add three elements to the top of the stack using the push() method. We then use the peek() method to retrieve the top element from the stack and store it in the topElement variable.

isEmpty():

The isEmpty() method in Java checks if the stack is empty. Here's an example:

Stack<Integer> stack = new Stack<Integer>();
boolean isEmpty = stack.isEmpty();

In this example, we create a new Stack object and use the isEmpty() method to check if the stack is empty. The isEmpty variable will be true if the stack is empty, and false otherwise.

 

Stack Example in Java

Now we will take an example of the stack in Java to understand the concept clearly.

import java.util.Stack;

public class StackExample {
   public static void main(String[] args) {
      // Creating a Stack
      Stack<Integer> stack = new Stack<Integer>();
      
      // Pushing elements to the Stack
      stack.push(1);
      stack.push(2);
      stack.push(3);
      stack.push(4);
      
      // Printing the Stack
      System.out.println("Stack: " + stack);
      
      // Popping elements from the Stack
      int element1 = stack.pop();
      int element2 = stack.pop();
      
      // Printing the Stack after popping elements
      System.out.println("Stack after popping two elements: " + stack);
      
      // Peeking at the top element of the Stack
      int topElement = stack.peek();
      
      // Printing the top element of the Stack
      System.out.println("Top element of the Stack: " + topElement);
      
      // Checking if the Stack is empty
      boolean isEmpty = stack.isEmpty();
      
      // Printing if the Stack is empty or not
      System.out.println("Is the Stack empty? " + isEmpty);
   }
}

Output:

Stack: [1, 2, 3, 4]
Stack after popping two elements: [1, 2]
Top element of the Stack: 2
Is the Stack empty? false

In this example, we first create a new Stack object using the java.util.Stack class. We then use the push() method to add four integers to the Stack. Next, we print the Stack using the System.out.println() method, which will output the elements of the Stack in the order they were added. We then use the pop() method to remove the top two elements from the Stack, and print the Stack again to see the changes.

After that, we use the peek() method to retrieve the top element of the Stack without removing it, and print the result.

Finally, we use the isEmpty() method to check if the Stack is empty or not, and print the result.

 

What is a Queue in Java?

A Queue in Java is a linear data structure that follows the First-In-First-Out (FIFO) principle, meaning that the element added earliest is removed first. It is an interface in the Java Collections Framework, belonging to the java.util package. Queues are commonly used for tasks like scheduling processes, managing requests in a web server, and simulating real-world scenarios such as waiting lines.

 

Different Queue Operations

The Queue interface in Java provides various methods to perform operations on a queue. The following are the basic operations that can be performed on a queue:

  • add(E element): Adds the specified element to the rear end of the queue. If the queue is full, an exception is thrown.
  • offer(E element): Adds the specified element to the rear end of the queue. If the queue is full, it returns false.
  • remove(): Removes and returns the element at the front end of the queue. If the queue is empty, an exception is thrown.
  • poll(): Removes and returns the element at the front end of the queue. If the queue is empty, it returns null.
  • element(): Returns the element at the front end of the queue without removing it. If the queue is empty, an exception is thrown.
  • peek(): Returns the element at the front end of the queue without removing it. If the queue is empty, it returns null.
  • isEmpty(): Checks if the queue is empty.

 

Queue Example in Java

Here's an example of a queue implementation in Java using the LinkedList class, which implements the Queue interface:

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {
   public static void main(String[] args) {
      Queue<Integer> queue = new LinkedList<Integer>();
      
      // Adding elements to the queue
      queue.add(1);
      queue.add(2);
      queue.add(3);
      queue.offer(4);
      
      // Removing elements from the queue
      int frontElement = queue.remove();
      Integer rearElement = queue.poll();
      
      // Displaying the front and rear elements
      System.out.println("Front element is: " + frontElement);
      System.out.println("Rear element is: " + rearElement);
      
      // Displaying the element at the front without removing it
      System.out.println("Element at the front is: " + queue.element());
      
      // Displaying if the queue is empty or not
      System.out.println("Is the queue empty? " + queue.isEmpty());
   }
}

Output:

Front element is: 1
Rear element is: 2
Element at the front is: 3
Is the queue empty? false

In this example, we first create a LinkedList object and assign it to a Queue reference. We then add four integers to the queue using the add() and offer() methods. We then remove two elements from the queue using the remove() and poll() methods. Finally, we display the front and rear elements of the queue using the element() and peek() methods, respectively, and check if the queue is empty using the isEmpty() method.

 

Stack Vs Queue - Differences and Comparison

A Stack and a Queue are both linear data structures, but they have different ways of managing elements. Here are the key differences between them:

Parameter Stack Data Structure Queue Data Structure
Access principle Follows the Last-In-First-Out (LIFO) principle, where the most recently added element is removed first. Follows the First-In-First-Out (FIFO) principle, where the element added earliest is removed first.
Insertion and deletion Both insertion (push) and deletion (pop) operations occur at the top. Insertion (enqueue) occurs at the rear, while deletion (dequeue) happens at the front.
Use cases Often used for tasks like parsing expressions, evaluating function calls, and managing memory allocation in programming languages. Commonly used for tasks such as scheduling processes, managing requests in a web server, and simulating real-world scenarios like waiting lines.
Implementation Can be implemented using an array or a linked list, with a pointer keeping track of the top element. Can be implemented using an array or a linked list, with pointers keeping track of the front and rear elements.
Variations Variations include Double Stack (two stacks sharing the same storage) and Min/Max Stack (maintaining minimum/maximum element information). Variations include Priority Queue (elements have priorities), Double-Ended Queue (deque, insertions and deletions at both ends), and Circular Queue (rear and front pointers wrap around).
Performance Push and pop operations generally have O(1) time complexity. Enqueue and dequeue operations typically have O(1) time complexity.
Real-life analogy Like a stack of plates, where you can only add or remove a plate from the top. Like a queue of people waiting in line, where the person who has been waiting the longest is served first, and new people join at the end of the line.

 

Summary

A Stack and a Queue are linear data structures in Java, differing primarily in their access principles, use cases, and operations. Stacks operate on a Last-In-First-Out (LIFO) basis, meaning that the most recently added element is removed first. Stacks are well-suited for tasks like parsing expressions, managing function calls, and memory allocation. Both insertion (push) and deletion (pop) operations occur at the top of the stack and have O(1) time complexity. They can be implemented using arrays or linked lists, with a pointer tracking the top element. Stack variations include Double Stack and Min/Max Stack.

On the other hand, Queues follow a First-In-First-Out (FIFO) principle, where the earliest added element is removed first. Queues are ideal for scheduling processes, managing web server requests, and simulating real-world scenarios like waiting lines. In a queue, insertion (enqueue) takes place at the rear, while deletion (dequeue) occurs at the front. Both operations have O(1) time complexity. Queues can be implemented using arrays or linked lists, with pointers to front and rear elements. Queue variations include Priority Queue, Double-Ended Queue (deque), and Circular Queue. In summary, the main differences between a Stack and a Queue in Java lie in their access principles, operations, and use cases.

 

Further Readings

Stack in java 
Queue in Java

 

Bashir Alam

Bashir Alam

He is a Computer Science graduate from the University of Central Asia, currently employed as a full-time Machine Learning Engineer at uExel. His expertise lies in Python, Java, Machine Learning, OCR, text extraction, data preprocessing, and predictive models. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment