Java Collections asLifoQueue() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 6+

Definition

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.

Syntax

Wrap any Deque, usually an ArrayDeque.

Java
Deque<String> deque = new ArrayDeque<>();
Queue<String> stack = Collections.asLifoQueue(deque);
stack.add("a");   // deque.addFirst("a")
stack.poll();     // deque.pollFirst()
CallWhat it does
stack.add(e)Same as deque.addFirst(e).
stack.offer(e)Same as deque.offerFirst(e).
stack.poll() / stack.remove()Same as pollFirst() / removeFirst().
stack.peek() / stack.element()Same as peekFirst() / getFirst().

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

ParameterDescription
dequeThe deque that actually stores the elements. Must not be null.

Return Value

Return ValueDescription
Queue<T>A LIFO queue view backed by deque.

Compare with Similar Methods

Related adapters and views:

MethodHow it differs
deque.push(e)Stack-style add, called on the deque itself.
newSetFromMap(m)Another adapter: a Set view of a Map.
reverseOrder()For a priority queue that returns the largest first.

Java Examples

Example 1 — Add Three, Take Back the Newest First

Fill the view with add and offer, then peek, poll, and remove, and check what is left in the deque.

Java
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;

public class CollectionsAsLifoQueueBasic {
    public static void main(String[] args) {
        Deque<String> deque = new ArrayDeque<>();
        Queue<String> stack = Collections.asLifoQueue(deque);

        stack.add("a");
        stack.add("b");
        stack.offer("c");
        System.out.println("view: " + stack);
        System.out.println("peek: " + stack.peek());
        System.out.println("poll: " + stack.poll() + ", remove: " + stack.remove());
        System.out.println("deque left: " + deque);
    }
}

How It Works

  1. Adds go to the front. Each add or offer calls addFirst/offerFirst, so the newest element "c" is at the head.
  2. Newest out first. peek shows "c"; poll takes "c" and remove takes "b".
  3. 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>())));
    }
}

How It Works

  1. One algorithm. walk takes the next node with poll and adds its children with add; it never knows which kind of queue it got.
  2. FIFO = breadth first. A plain ArrayDeque used as a queue finishes level one (A, B) before visiting A1 and A2.
  3. 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());
        }
    }
}

How It Works

  1. Deque to view. push on the deque puts "x" at the front, and the view's peek sees it immediately.
  2. View to deque. add on the view becomes addFirst, so the deque prints [y, x].
  3. 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.

Next: binarySearch()

Find an element in a sorted list quickly.

binarySearch() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful