1. The Java Deque Interface
The Java Deque (Double-Ended-Queue) interface extends the Queue interface and Java’s LinkedList implements Deque interface. In the past coding examples, we used the LinkedList as queue via the Java Queue Interface. In this example, we will use our LinkedList using the Deque Interface. Unlike queue, we can add and remove items from both ends of the LinkedList via Java Deque Interface. The Deque is also called as ‘Deck’.
2. Deque Interface Methods
Before we go into the methods details, let us say the LinkedList contains three items in it. The below picture shows the existing items and its order:

2.1 addFirst & addLast
We can add items to both ends of the list while using Deque Interface. The addFirst method adds an item in the head of the LinkedList and addLast method adds an item towards the tail end. In the below picture item E5 and E4 are added using these deque interface methods:

2.2 removeFirst & removeLast
In Deck or Java Deque, we can also remove elements from both the end of the queue. The removeFirst method removes an item from the front of the queue and removeLast method removes an item from the tail end of the queue. Note, here our linked list is acting as double-ended-queue. In the below picture, element E4 and E5 are discarded from the queue using these methods:

2.3 getFirst & getLast Methods
The getFirst and getLast methods are used to get the elements from the front or back end of the queue for inspecting it. These methods will not remove the elements from the list.
Now, we have some background of the methods supported by the Java’s Deque Interface & it is time to look at the example which operates the linked list via deque interface.
3. LinkedList as Deque – Code Example
3.1 The Product Class
There is no change in the product class. You can refer about the Product
class here: ArrayList
3.2 Create a LinkedList Deque & Add Items
First, we create a LinkedList
and store its reference in a
Deque (Line No 2). This means we are going to operate this LinkedList
as Deque. Then we create six Products
and store those in the Deck. Note, we use both
addFirst and
addLast methods. Finally, the Deque will have items in the following order: Clips, Pencil, Pen, Rubber, Writing Pad, Sharpener.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Sample01: Create LinkedList & use it as Queue Deque<Product> Products = new LinkedList<Product>(); //Sample 02: Construct Sample Products Product p1 = new Product(101, "Pen"); Product p2 = new Product(102, "Pencil"); Product p3 = new Product(103, "Rubber"); Product p4 = new Product(104, "Writing Pad"); Product p5 = new Product(105, "Clips"); Product p6 = new Product(106, "Sharpner"); //Sample 04: Add Product to Deck (Both Ends) Products.addFirst(p1); Products.addFirst(p2); Products.addLast(p3); Products.addLast(p4); Products.addFirst(p5); Products.addLast(p6); printProducts(Products); |
3.3 Examine Element in the Java Deque
Sometimes we may need to examine the elements without removing it from the underlying data structure. In Deque
, we can get the head element by calling
getFirst and tail element by calling the
getLast functions. Note, these methods will not remove the items from the Deque. In the below code, we retrieve the first and last product and print them in the console window:
1 2 3 4 5 |
//Sample 05: Examine element at both end Product Head = Products.getFirst(); Product Tail = Products.getLast(); System.out.println("Head is : " + Head); System.out.println("Tail is : " + Tail); |
3.4 Removing Elements from Deque
The method
removeFirst discards the current head item from the Deque. Whereas the
removeLast wipes the tail element. Since
Deque is Double Ended, we can add and remove elements from both the of the structure. In the below code, we remove two head items and one tail item from our Products
Deque:
1 2 3 4 5 |
//Sample 06: Remove two Heads and One Tail Products.removeFirst(); Products.removeFirst(); Products.removeLast(); printProducts(Products); |
5. Complete Example of Java Deque
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.Deque; import java.util.LinkedList; public class LinkedListTest { public static void main(String[] args) { //Sample01: Create LinkedList & use it as Queue Deque<Product> Products = new LinkedList<Product>(); //Sample 02: Construct Sample Products Product p1 = new Product(101, "Pen"); Product p2 = new Product(102, "Pencil"); Product p3 = new Product(103, "Rubber"); Product p4 = new Product(104, "Writing Pad"); Product p5 = new Product(105, "Clips"); Product p6 = new Product(106, "Sharpner"); //Sample 04: Add Product to Deck (Both Ends) Products.addFirst(p1); Products.addFirst(p2); Products.addLast(p3); Products.addLast(p4); Products.addFirst(p5); Products.addLast(p6); printProducts(Products); //Sample 05: Examine element at both end Product Head = Products.getFirst(); Product Tail = Products.getLast(); System.out.println("Head is : " + Head); System.out.println("Tail is : " + Tail); //Sample 06: Remove two Heads and One Tail Products.removeFirst(); Products.removeFirst(); Products.removeLast(); printProducts(Products); } //Sample 03: List the Products private static void printProducts(Deque<Product> Products) { System.out.println("Product Details:-"); for(Product prod: Products){ System.out.println(prod); } System.out.println("=================="); } } |
Watch this Example as a Presentation in the Below tube video.
Categories: Java
Tags: addFirst, addLast, Deck, Deque, removeFirst, removeLast