- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Collections asLifoQueue() Method
Description
The Java Collections asLifoQueue(Deque<T>) method is used to get a view of a Deque as a Last-in-first-out (Lifo) Queue.
Declaration
Following is the declaration for java.util.Collections.asLifoQueue() method.
public static <T> Queue<T> asLifoQueue(Deque<T> deque)
Parameters
deque − This is the deque.
Return Value
The method call returns the queue.
Exception
NA
Getting LIFO Queue from a Collection of Integers Example
The following example shows the usage of Java Collection asLifoQueue(Deque) method to get a Lifo Queue. We've created a Deque object with some integers. Using asLifoQueue(Deque) method, we get the queue then printed the queue.
package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;
public class CollectionsDemo {
public static void main(String[] args) {
// create an array deque
Deque<Integer> deque = new ArrayDeque<>(Arrays.asList(20,30,20,30,15,22,11));
// get queue from the deque
Queue<Integer> nq = Collections.asLifoQueue(deque);
System.out.println("View of the queue is: "+nq);
}
}
Output
Let us compile and run the above program, this will produce the following result.
View of the queue is: [20, 30, 20, 30, 15, 22, 11]
Getting LIFO Queue from a Collection of Strings Example
The following example shows the usage of Java Collection asLifoQueue(Deque) method to get a Lifo Queue. We've created a Deque object with some strings. Using asLifoQueue(Deque) method, we get the queue then printed the queue.
package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;
public class CollectionsDemo {
public static void main(String[] args) {
// create an array deque
Deque<String> deque = new ArrayDeque<>(Arrays.asList("Welcome","to","Tutorialspoint"));
// get queue from the deque
Queue<String> nq = Collections.asLifoQueue(deque);
System.out.println("View of the queue is: "+nq);
}
}
Output
Let us compile and run the above program, this will produce the following result.
View of the queue is: [Welcome, to, Tutorialspoint]
Getting LIFO Queue from a Collection of Objects Example
The following example shows the usage of Java Collection asLifoQueue(Deque) method to get a Lifo Queue. We've created a Deque object with some student objects. Using asLifoQueue(Deque) method, we get the queue then printed the queue.
package com.tutorialspoint;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;
public class CollectionsDemo {
public static void main(String[] args) {
// create an array deque
Deque<Student> deque = new ArrayDeque<>(Arrays.asList(new Student(1, "Julie"),
new Student(2, "Robert"), new Student(3, "Adam")));
// get queue from the deque
Queue<Student> nq = Collections.asLifoQueue(deque);
System.out.println("View of the queue is: "+nq);
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
}
Output
Let us compile and run the above program, this will produce the following result.
View of the queue is: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]