Collections static Queue asLifoQueue(Deque deque) method Example Program


Returns 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.

Program

package com.candidjava;

import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;

/**
 * @author : karthikeyan.T
 * @description : the following code shows how to use
 *              Collections.asLifoQueue(Deque <T> deque) method.
 * */

public class LIFO {
	public static void main(String args[]) {
		Deque<String> dq = new ArrayDeque<String>(7);
		dq.add("karthik");
		dq.add("kamal");
		dq.add("hari");
		dq.add("mohan");
		dq.add("vinodh");
		Queue<String> nq = Collections.asLifoQueue(dq);
		System.out.println("View of the queue is : " + nq);
	}
}

Output

View of the queue is : [karthik, kamal, hari, mohan, vinodh]

Explanation

public static <T> Queue<T> asLifoQueue(Deque<T> deque)
Returns 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.

Parameters:
deque - the deque
Returns:
the queue
Since:
1.6


Related Post

Comments


©candidjava.com