Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Java Deque via LinkedList

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:

Initial Deque Linked List
Initial Deque Linked List

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:

adding Elements on Both End of LinkedList Deque
adding Elements on Both End of LinkedList Deque

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:

Removing elements from Both End of the Deque
Removing elements from Both End of the Deque

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.

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:

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:

5. Complete Example of Java Deque

Watch this Example as a Presentation in the Below tube video.

Categories: Java

Tags: , , , , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.