In the realm of data structures, stacks and queues are two fundamental concepts that every Java developer should be familiar with. Both are abstract data types that represent collections designed for holding elements, but they differ in the order in which they add, remove, and access elements.

Before diving into the Java code for stacks and queues, let's briefly discuss these data structures and their real-world applications. A stack follows the Last-In-First-Out (LIFO) principle, which means the last element added to the stack is the first one to be removed. It's like a stack of plates; you can only take the top plate from the stack. Queues, on the other hand, follow the First-In-First-Out (FIFO) principle. It's like a line of people waiting for a bus; the person who gets in line first will be the first one to get on the bus.

Stacks in Java
In Java, the java.util.Stack class is a standard implementation of the stack data structure. It extends the Vector class and provides a last-in-first-out (LIFO) stack of objects.

However, it's essential to note that the java.util.Stack class is part of the Java Collections Framework, but it'sīŋŊ discouraged for use. It's recommended to use the java.util.deque interface (and its implementations) instead, as it provides more extensive functionality and is part of the Java Collections Frameworkgets more extensive functionality and is part of the Java Collections Framework.
Basic Stack Operations

Here's a simple Java example using the java.util.Deque interface for basic stack operations:
import java.util.Deque;
import java.util.ArrayDeque;
public class StackExample {
public static void main(String[] args) {
Deque<String> stack = new ArrayDeque<String>();
stack.push("First");
stack.push("Second");
stack.push("Third");
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
}
}
This example demonstrates the following stack operations:
- push: adds an element to the top of the stack
- pop: removes and returns the top element of the stack
- isEmpty: checks if the stack is emptyStack Applications: Reverse a String

A classic use case for stacks is to reverse a string. Here's how you can achieve this:
public String reverseString(String s) {
Deque<Character> stack = new ArrayDeque<Character>();
for (char c : s.toCharArray()) {
stack.push(c);
}
StringBuilder reversed = new StringBuilder();
while (!stack.isEmpty()) {
reversed.append(stack.pop());
}
return reversed.toString();
}
Queues in Java
The java.util.Queue interface is a standard implementation of the queue data structure. It extends the java.util.List interface and provides a first-in-first-out (FIFO) queue of objects.

There are multiple implementations of the java.util.Queue interface, such as java.util.LinkedList and java.util.PriorityQueue. The most straightforward implementation is java.util.LinkedList, which uses a doubly-linked list internally to store elements.
Basic Queue Operations



















Here's a Java example using the java.util.Queue interface for basic queue operations:
import java.util.Queue;
import java.util.LinkedList;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
queue.offer("First");
queue.offer("Second");
queue.offer("Third");
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
This example demonstrates the following queue operations:
- offer: adds an element to the tail of the queue
- poll: removes and returns the head element of the queue
- isEmpty: checks if the queue is emptyQueue Applications: Print Even Indexed Numbers
A common interview question involves using queues to print even and odd indexed elements of an array in two separate threads. Here's an example of how you can achieve this using the java.util.concurrent.BlockingQueue:
//... (implement even thread, odd thread, and main class)
In conclusion, understanding stacks and queues is crucial for Java developers, as these data structures have wide-ranging applications in various aspects of programming. By mastering these fundamental concepts, you'll be well-equipped to tackle a broad range of coding challenges. So, keep practicing and exploring the intricacies of these data structures to enhance your Java skills!