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 scale and paint an image in Swing? - December 1, 2026
- How do I arrange components using BorderLayout in Java Swing? - December 1, 2026
- How do I map a Java class to a database table? - November 30, 2026