How do I remove elements from Deque?

This example shows you how to remove some elements from the Deque object. We can use the following methods for removing elements from Deque: remove(), remove(Object o), removeFirst(), removeLast().

package org.kodejava.util;

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

public class RemoveDequeDemo {
    public static void main(String[] args) {
        Deque<String> deque = new LinkedList<>();
        deque.add("A");
        deque.add("B");
        deque.add("C");
        deque.add("D");
        deque.add("E");
        deque.add("F");

        // Removes and retrieves the head of this Deque
        deque.remove();      // Removes "A"

        // Removes the first occurrence of element from this Deque
        deque.remove("F");   // Removes "F"

        // Retrieves and removes the first element of this deque
        deque.removeFirst(); // Removes "B"

        // Retrieves and removes the last element of this deque
        deque.removeLast();  // Removes "E"

        for (String item : deque) {
            System.out.println("Item = " + item);
        }
    }
}

How do I add elements into a Deque?

To add elements into a Deque object we can use the add(), addFirst() and addLast() method call.

package org.kodejava.util;

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

public class DequeAddElement {
    public static void main(String[] args) {
        // Create an instance of Deque using LinkedList class.
        Deque<String> deque = new LinkedList<>();

        // Insert a word into the Deque
        deque.add("jumps");

        // Insert words at the beginning of the current Deque
        // elements
        deque.addFirst("fox");
        deque.addFirst("brown");
        deque.addFirst("quick");
        deque.addFirst("The");

        // Insert words at the end of the current Deque elements
        deque.addLast("over");
        deque.addLast("the");
        deque.addLast("lazy");
        deque.addLast("dog");

        for (String word : deque) {
            System.out.println("Word = " + word);
        }
    }
}

Here is the output:

Word = The
Word = quick
Word = brown
Word = fox
Word = jumps
Word = over
Word = the
Word = lazy
Word = dog

How do I create Deque using ArrayDeque class?

This example show you how to create a Deque using the ArrayDeque implementation. The ArrayDeque stores its elements using an array. If the number of elements exceeds the space in the array, a new array will be allocated, and all elements moved the new allocated array.

In the code below we initiate the size of the Deque to store five elements. When we add the element number six the array that stores the elements of the Deque will be resized.

package org.kodejava.util;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
    public static void main(String[] args) {
        // Constructs an empty array deque with an initial capacity
        // sufficient to hold the specified number of elements.
        Deque<Integer> deque = new ArrayDeque<>(5);
        deque.add(1);
        deque.add(1);
        deque.add(2);
        deque.add(3);
        deque.add(5);
        deque.add(8);
        deque.add(14);
        deque.add(22);

        for (Integer number : deque) {
            System.out.println("Number = " + number);
        }
    }
}

How do I remove the first or the last found element from Deque?

This example demonstrates the use of Deque.removeFirstOccurrence() method call to remove the first occurrences of an element in the Deque object and Deque.removeLastOccurrence() method call to remove the last occurrences of an element in the Deque object.

package org.kodejava.util;

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

public class DequeRemoveDemo {
    public static void main(String[] args) {
        Deque<String> deque1 = new LinkedList<>();
        deque1.offer("a");
        deque1.offer("b");
        deque1.offer("c");
        deque1.offer("d");
        deque1.offer("e");
        deque1.offer("d");

        Deque<String> deque2 = new LinkedList<>(deque1);

        // Removes the first occurrence of letter "d" from deque1
        deque1.removeFirstOccurrence("d");

        Iterator<String> first = deque1.iterator();
        System.out.println("After removeFirstOccurrence(Object o): ");
        System.out.println("=======================================");
        while (first.hasNext()) {
            System.out.println(first.next());
        }

        // Removes the last occurrence of letter "d" from deque2
        deque2.removeLastOccurrence("d");
        Iterator<String> last = deque2.iterator();
        System.out.println("After removeLastOccurrence(Object o): ");
        System.out.println("========================================");
        while (last.hasNext()) {
            System.out.println(last.next());
        }
    }
}

The output of the code snippet above is:

After removeFirstOccurrence(Object o): 
=======================================
a
b
c
e
d
After removeLastOccurrence(Object o): 
========================================
a
b
c
d
e

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.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<>();
        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);

        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);
    }
}

The output of our code snippet is:

Items IN in order : one,two,three,four
Items OUT in order: four,three,two,one