In Java, Collections.asLifoQueue(deque) returns a Queue view of a Deque that behaves last-in-first-out (LIFO). Normally a queue hands back the oldest element first; this view hands back the newest.
Use it when some code only accepts a Queue but you want stack behavior. The view stores nothing itself: every call goes straight to the deque you passed in.
A static method of the java.util.Collections utility class, available since Java 6. Call it on the class name, as in Collections.asLifoQueue(...), after import java.util.Collections;. You never create a Collections object.
Added in Java 6, together with the Deque interface. If you control the code, calling push, pop, and peek on the Deque directly is simpler; asLifoQueue exists for APIs typed as Queue.
Library declarations
Java
// java.util.Collections
public static <T> Queue<T> asLifoQueue(Deque<T> deque)
Parameters
Parameter
Description
deque
The deque that actually stores the elements. Must not be null.
view: [c, b, a]
peek: c
poll: c, remove: b
deque left: [a]
How It Works
Adds go to the front. Each add or offer calls addFirst/offerFirst, so the newest element "c" is at the head.
Newest out first.peek shows "c"; poll takes "c" and remove takes "b".
Shared storage. Printing the original deque shows the single remaining element, "a".
Example 2 — Same Loop, Two Walk Orders
A tree walker only knows about Queue. A normal FIFO queue gives breadth-first order; the LIFO view gives depth-first order.
Java
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.TreeMap;
public class CollectionsAsLifoQueueWalk {
static String walk(Map<String, List<String>> tree, Queue<String> todo) {
StringBuilder order = new StringBuilder();
todo.add("root");
while (!todo.isEmpty()) {
String node = todo.poll();
order.append(node).append(' ');
List<String> kids = tree.get(node);
if (kids != null) {
for (String k : kids) {
todo.add(k);
}
}
}
return order.toString().trim();
}
public static void main(String[] args) {
Map<String, List<String>> tree = new TreeMap<>();
tree.put("root", Arrays.asList("A", "B"));
tree.put("A", Arrays.asList("A1", "A2"));
System.out.println("FIFO: " + walk(tree, new ArrayDeque<String>()));
System.out.println("LIFO: " + walk(tree, Collections.asLifoQueue(new ArrayDeque<String>())));
}
}
Output
FIFO: root A B A1 A2
LIFO: root B A A2 A1
How It Works
One algorithm.walk takes the next node with poll and adds its children with add; it never knows which kind of queue it got.
FIFO = breadth first. A plain ArrayDeque used as a queue finishes level one (A, B) before visiting A1 and A2.
LIFO = depth first. Through asLifoQueue, the child added last (B, then A2) comes out first, so the walk dives down instead of across.
Example 3 — Deque and Queue View Stay in Sync
Push on the deque, add on the view, then drain everything and call remove() on the empty view.
Java
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.NoSuchElementException;
import java.util.Queue;
public class CollectionsAsLifoQueueView {
public static void main(String[] args) {
Deque<String> deque = new ArrayDeque<>();
Queue<String> lifo = Collections.asLifoQueue(deque);
deque.push("x");
System.out.println("after deque.push(x), view peek = " + lifo.peek());
lifo.add("y");
System.out.println("after view.add(y), deque = " + deque);
String a = lifo.poll();
String b = lifo.poll();
String c = lifo.poll();
System.out.println("poll: " + a + ", " + b + ", then " + c);
try {
lifo.remove();
} catch (NoSuchElementException e) {
System.out.println("remove on empty: " + e.getClass().getSimpleName());
}
}
}
Output
after deque.push(x), view peek = x
after view.add(y), deque = [y, x]
poll: y, x, then null
remove on empty: NoSuchElementException
How It Works
Deque to view.push on the deque puts "x" at the front, and the view's peek sees it immediately.
View to deque.add on the view becomes addFirst, so the deque prints [y, x].
Empty behavior. The third poll returns null, but remove() maps to removeFirst() and throws NoSuchElementException.
Frequently Asked Questions
It returns a Queue view of a Deque in which add, offer, poll, remove, peek, and element all work on the front, giving last-in-first-out order.
No. It is a view; every operation goes to the deque you passed, and changes are visible both ways.
When an existing method or class only accepts a Queue but you want stack (LIFO) behavior, for example to turn a breadth-first walk into a depth-first one.
Java 6, the same release that added the Deque interface and ArrayDeque.
poll and peek return null; remove and element throw NoSuchElementException.