Java Stack & Queue
In this page:
Working with Stack (LIFO)
Stack (a legacy Vector subclass) provides LIFO (last-in, first-out) behavior through push() and pop() — the most recently added element is always the first one removed, like a stack of plates.
Example: Working with Stack (LIFO)
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
System.out.println(stack.pop()); // 2: last in, first out
}
}
Login to try C/C++/Java/PHP code in the editor
Stack Search and Size
Stack also supports search() to find an element's 1-based distance from the top, and size()/isEmpty() for capacity checks — useful when you need to inspect the stack without popping through it.
Example: Stack Search and Size
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(10);
stack.push(20);
System.out.println(stack.search(10)); // 1-based distance from top
System.out.println(stack.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Working with Queue (FIFO)
Queue provides FIFO (first-in, first-out) behavior through offer() and poll() — the first element added is always the first one removed, like people waiting in line.
Example: Working with Queue (FIFO)
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
System.out.println(queue.poll()); // 1: first in, first out
}
}
Login to try C/C++/Java/PHP code in the editor
Modern Stack and Queue with Deque
ArrayDeque is generally preferred over the older Stack and LinkedList-as-queue for both roles today, since it's faster and was explicitly designed as a modern double-ended queue implementation.
Example: Modern Stack and Queue with Deque
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>(); // preferred over Stack/LinkedList
deque.push(1);
deque.push(2);
System.out.println(deque.pop());
}
}
Login to try C/C++/Java/PHP code in the editor
Handling Queue Edge Cases
Calling pop()/poll() on an empty stack or queue either throws an exception or returns null depending on which method you use — check isEmpty() first, or use the peek/poll variants that return null instead of throwing.
Example: Handling Queue Edge Cases
import java.util.Queue;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
System.out.println(queue.poll()); // null: empty queue, no exception
System.out.println(queue.isEmpty());
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting