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 iterate LinkedList elements using Iterator?

This example show you how to use the Iterator interface to iterate elements of a LinkedList.

package org.kodejava.util;

import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;

public class LinkedListIterator {
    public static void main(String[] args) {
        List<String> grades = new LinkedList<>();
        grades.add("A");
        grades.add("B");
        grades.add("C");
        grades.add("D");
        grades.add("E");

        // Iterates elements in LinkedList using Iterator.
        Iterator<String> iterator = grades.iterator();
        while (iterator.hasNext()) {
            System.out.println("Grade: " + iterator.next());
        }
    }
}

This program prints the following output:

Grade: A
Grade: B
Grade: C
Grade: D
Grade: E