Java Utility Library

Java Collections - asLifoQueue() Method



The java.util.Collections.asLifoQueue() method returns a view of a Deque as a Last-in-first-out (Lifo) Queue.

Syntax

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

Here, T is the type of element to add and of the collection.


Parameters

deque Specify the deque.

Return Value

Returns the queue.

Exception

NA.

Example:

In the example below, the java.util.Collections.asLifoQueue() method is used to add all of the specified elements to the specified collection.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Deque object
    Deque<Integer> deque = new ArrayDeque<Integer>();

    //populating the deque
    deque.add(10);
    deque.add(20);
    deque.add(30);
    deque.add(40);
    deque.add(50);

    //creating a view of a Deque as a 
    //Last-in-first-out (Lifo) Queue.
    Queue queue = Collections.asLifoQueue(deque);

    //printing queue
    System.out.println("queue contains: " + queue);     
  }
}

The output of the above code will be:

queue contains: [10, 20, 30, 40, 50]

❮ Java.util - Collections