Home  >  Core Java

Collections.asLifoQueue in Java

By Arvind Rai, February 02, 2022
Find the Java doc of Collections.asLifoQueue method.
static <T> Queue<T> asLifoQueue(Deque<T> deque) 
1. The asLifoQueue method returns a Last-in-first-out (Lifo) Queue for the specified Deque. The Deque allows element insertion and removal at both ends.
2. The asLifoQueue is useful to create a Lifo Queue when required.
3. The asLifoQueue accepts a Deque and returns a Queue instance.



Example-1

CollectionsAsLifoQueue1.java
package com.concretepage;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;

public class CollectionsAsLifoQueue1 {
  public static void main(String[] args) {
    Deque<String> arrayDeque = new ArrayDeque<>();
    arrayDeque.add("A");
    arrayDeque.add("B");
    arrayDeque.add("C");

    Queue<String> queue = Collections.asLifoQueue(arrayDeque);
    
    queue.add("D");
    System.out.println(queue);
    System.out.println("queue.poll(): " + queue.poll());
 }
} 
Output
[D, A, B, C]
queue.poll(): D 

Example-2

CollectionsAsLifoQueue2.java
package com.concretepage;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;

public class CollectionsAsLifoQueue2 {
  public static void main(String[] args) {
    Deque<Integer> arrayDeque = new ArrayDeque<>();
    arrayDeque.add(10);
    arrayDeque.add(20);
    arrayDeque.add(30);

    Queue<Integer> queue = Collections.asLifoQueue(arrayDeque);
    
    queue.add(40);
    System.out.println(queue);
    System.out.println("queue.poll(): " + queue.poll());
  }
} 
Output
[40, 10, 20, 30]
queue.poll(): 40 

Reference

Java Collections
POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us