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
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024