Collections asLifoQueue() method in Java with Examples

The asLifoQueue() method of java.util.Collections class is used to return a view of a Deque as a Last-in-first-out (Lifo) Queue. Method add is mapped to push, remove is mapped to pop and so on. This view can be useful when you would like to use a method requiring a Queue but you need Lifo ordering.

Each method invocation on the queue returned by this method results in exactly one method invocation on the backing deque, with one exception. The addAll method is implemented as a sequence of addFirst invocations on the backing deque.

Syntax:



public static Queue asLifoQueue(Deque deque)

Parameters: This method takes deque as a parameter which is to be converted into a LifoQueue.

Return Value: This method returns a LifoQueue from the deque.

Below are the examples to illustrate the asLifoQueue() method

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// asLifoQueue() method
  
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);
  
            // printng the Queue
            System.out.println("View of the queue is: " + nq);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// asLifoQueue() method
  
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);
  
            // printng the Queue
            System.out.println("View of the queue is: " + nq);
        }
        catch (IllegalArgumentException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai