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 ]]
java_util_collections.htm
Advertisements