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
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023