Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Deque Stack Queue

farenda 2015-06-29 0

Problem:

In Java Deque can be used as LIFO Queue, which is a Stack. In this Java tutorial we’ll explain how it works and show how to use JDK API effectively!

Solution:

Standard java.util.Deque is a LILO (Last In, Last Out) structure, which means that items added as last, will be removed as last. Contrary to that is LIFO (Last In, First Out), which means that item added as last will be removed as the first, so it’s a Stack.

In JDK there is a java.util.Stack class, but it should not be used because it extends a java.util.Vector, which is a list and lists are not LIFO, but LILO. And anyone can pass it as a Vector to methods that will operate on it as on any other Vector. Uncool. :-|

The following example illustrates how to turn LinkedList, which is an implementation of Deque into a Stack:

package com.farenda.java;

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

public class CollectionsAsLifoQueue {

    public static void main(String[] args) {
        // Deque is LILO (Last In Last Out)
        Deque<String> items = new LinkedList<>();

        // In this Deque items are added at the end:
        items.offer("a");
        items.offer("b");
        System.out.println("Added to Deque: " + items);

        // In this Deque items are removed from the front:
        items.poll();
        System.out.println("Removed from Deque: " + items);

        // View this Deque as a LIFO Queue, which is a Stack
        Queue<String> stack = Collections.asLifoQueue(items);

        // In Stack items are added at the front:
        stack.offer("c");
        System.out.println("Added to Stack: " + items);

        // And removed from the front:
        stack.poll();
        System.out.println("Removed from Stack: " + items);
    }
}

In the example you can see that LinkedList is a Deque and we (re)use java.util.Collections.asLifoQueue() method to view the list as a LIFO Queue, which is… a Stack!

Running the Deque as Stack example will print the following output:

Added to Deque: [a, b]
Removed from Deque: [b]
Added to Stack: [c, b]
Removed from Stack: [b]

Look at the order in which items are added removed from the structure. In case of Deque we have LILO, but when we use it as a Stack (LIFO Queue), it behaves as LIFO!

As usually, we’ve applied Item 47 from “Effective Java, 2nd” – Know and use the libraries! ;-)

Share with the World!
Categories Java Tags java-collections
Previous: Bash if else
Next: Java binary search

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok