How do I 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 org.kodejava.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 deque = new LinkedList();
        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());
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply