Collections asLifoQueue() method in Java with Examples Last Updated : 06 Jun, 2021 Comments Improve Suggest changes Like Article Like Report 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() methodExample 1: Java // 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); // 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] Example 2: Java // 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); // 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] Create Quiz Comment R rohitprasad3 Follow 0 Improve R rohitprasad3 Follow 0 Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like