Collections asLifoQueue() method in Java with Examples

Last Updated : 30 Jul, 2026

The Collections.asLifoQueue() method of the java.util.Collections class returns a Last-In-First-Out (LIFO) Queue view of the specified Deque. It allows a Deque to be used wherever a Queue is required while preserving stack (LIFO) behavior.

  • add() behaves like push(), and remove() behaves like pop().
  • Useful when a method accepts a Queue but requires LIFO ordering.
Java
import java.util.*;

public class GFG1 {
    public static void main(String[] argv) throws Exception
    {

        try {

            // creating object of Deque<Integer>
            Deque<Integer> deque = new ArrayDeque<Integer>(7);

            // Adding element to deque
            deque.add(1);
            deque.add(2);
            deque.add(3);
            deque.add(4);
            deque.add(5);

            // get queue from the deque
            // using asLifoQueue() method
            Queue<Integer> nq = Collections.asLifoQueue(deque);

            // printing the Queue
            System.out.println("View of the queue is: " + nq);
        }
        catch (IllegalArgumentException e) {

            System.out.println("Exception thrown : " + e);
        }
    }
}

Output
View of the queue is: [1, 2, 3, 4, 5]

Explanation: In this example, Collections.asLifoQueue() creates a LIFO queue view of the Deque. Since no insertion or removal is performed through the queue, both the queue and the deque display the same elements.

Syntax:

public static <T> Queue<T> asLifoQueue(Deque<T> deque)

  • Parameter: deque: The Deque to be viewed as a LIFO queue.
  • Returns: A Queue view that follows Last-In-First-Out (LIFO) ordering.

Example: Creating a LIFO Queue from a String Deque

Java
import java.util.*;

public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {

            // creating object of Deque<Integer>
            Deque<String> deque = new ArrayDeque<String>(7);

            // Adding element to deque
            deque.add("Ram");
            deque.add("Gopal");
            deque.add("Verma");

            // get queue from the deque
            // using asLifoQueue() method
            Queue<String> nq = Collections.asLifoQueue(deque);

            // printing the Queue
            System.out.println("View of the queue is: " + nq);
        }
        catch (IllegalArgumentException e) {

            System.out.println("Exception thrown : " + e);
        }
    }
}

Output
View of the queue is: [Ram, Gopal, Verma]

Explanation: In this example, a Deque<String> is converted into a LIFO queue. The returned queue shares the same underlying data as the original deque, so changes made through either collection are reflected in both.

Example: Demonstrating LIFO Behavior

Java
import java.util.*;

public class GFG {
    public static void main(String[] args) {

        Deque<Integer> deque = new ArrayDeque<>();

        Queue<Integer> queue = Collections.asLifoQueue(deque);

        queue.add(10);
        queue.add(20);
        queue.add(30);

        System.out.println("Queue: " + queue);
        System.out.println("Removed: " + queue.remove());
        System.out.println("Queue after removal: " + queue);
    }
}

Output
Queue: [30, 20, 10]
Removed: 30
Queue after removal: [20, 10]

Explanation: The queue follows Last-In-First-Out (LIFO) order. Although the Queue interface is used, add() inserts elements at the front of the backing Deque, and remove() removes the most recently added element. This demonstrates the stack-like behavior of asLifoQueue().

Advantages of Collections.asLifoQueue()

  • Allows a Deque to be used wherever a Queue is required.
  • Provides LIFO (Last-In-First-Out) behavior while using the Queue interface.
  • Does not create a copy of the Deque; it returns a backed view, saving memory.
  • Changes made to the returned queue are reflected in the original Deque, and vice versa.
  • Useful for implementing stack-like operations using the standard Queue interface.
Comment