Home  >  Core Java

Example of Collections.asLifoQueue in Java

By Arvind Rai, May 18, 2013
Collections.asLifoQueue in java has been introduced in JDK 1.6. Collections.asLifoQueue converts a Deque into lifo Queue. Lifo is last in first out. Deque is the collection in which insertion and removal is possible at both end.
CollectionsAsLifoQueueDemo.java
package com.concretepage.util;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.Queue;
public class CollectionsAsLifoQueueDemo {
    public static void main(String[] args) {
        //Insertion and deletion is possible at both end in Deque.
        Deque<String> arrayDeque = new ArrayDeque<String>();
        arrayDeque.add("AAAA");
        arrayDeque.add("BBBB");
        arrayDeque.addFirst("CCCC");
        arrayDeque.addLast("DDDD");
        for(String s: arrayDeque){
        	System.out.println(s);
        }
        Queue<String> queue = Collections.asLifoQueue(arrayDeque);
        System.out.println("queue.pop:"+queue.poll());
        System.out.println("queue.poll():"+queue.element());
    }
} 
Output
CCCC
AAAA
BBBB
DDDD
queue.pop:CCCC
queue.poll():AAAA
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us