java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to create a last in first out Deque?

This example show you how to create a LIFO (Last In First Out) Deque. We call the Deque.peekLast() method to get the last element from the Deque, poll the last element and repeat until all the elements read.

package com.javacoderanch.example.util;

import java.util.Deque;
import java.util.LinkedList;

public class DequeLIFODemo {

    public static void main(String[] args) {
        //
        // Create an instance of a Deque, here we use the LinkedList
        // class which implements the Deque interface.
        //
        Deque<String> deque = new LinkedList<String>();
        deque.add("one");
        deque.add("two");
        deque.add("three");
        deque.add("four");

        StringBuilder in = new StringBuilder("Items IN in order : ");

        //
        // Returns an iterator over the elements in this deque in
        // proper sequence
        //
        for (String s : deque) {
            in.append(s).append(",");
        }

        in.deleteCharAt(in.length() - 1);
        System.out.println(in.toString());

        StringBuilder out = new StringBuilder("Items OUT in order: ");
        for (int i = 0; i < deque.size();) {
            out.append(deque.peekLast()).append(",");

            //
            // Retrieves and removes the last element of this deque,
            // or returns null if this deque is empty.
            //
            deque.pollLast();
        }

        out.deleteCharAt(out.length() - 1);
        System.out.println(out.toString());
    }
}