The Java Deque interface, short for "Double Ended Queue," is part of the Java Collections Framework, and it resides in java.util package.
Important Key Points About Deque Interface
Double Ended:
As the name suggests, Deque is a queue in which we can insert and remove elements from both ends. It means you can perform operations at the front as well as at the rear.
Insertion and Removal Operations:
Deque supports the insertion of elements at both ends. The methods addFirst(E e), addLast(E e), offerFirst(E e), and offerLast(E e) are used for this purpose. Similarly, to remove elements, we use removeFirst(), removeLast(), pollFirst(), and pollLast().
Exception throwing and Non-Exception throwing methods:
Similar to the Queue, each operation in Deque has two methods: one throws an exception when the operation fails (addFirst, removeLast etc.), and the other returns a special value (null or false, depending on the operation) without throwing an exception (offerFirst, pollLast etc.).
Stack LIFO operations:
The Deque interface can also function as a stack (LIFO). The Deque implementations have methods for push, pop, and peek operations when used as a stack.
No Capacity Restrictions:
Deques generally do not have any capacity constraints and they can grow as needed based on the usage, but ArrayDeque has an exception, as it is array-based.
Null Elements:
Depending on the implementation, null elements may or may not be allowed. For example, LinkedList allows nulls, but ArrayDeque does not.
Iteration:
Deque interface provides an iterator that can iterate in both directions - regular order via iterator() and reverse order via descendingIterator().
Usage in real-world applications:
Deque is often used in algorithm design and data structure implementation where you need more flexibility than a traditional queue or stack.
Thread Safety:
Similar to Queue, Deque implementations (ArrayDeque, LinkedList) are not thread-safe. The ConcurrentLinkedDeque is a thread-safe Deque implementation provided by Java.
Deque interface Methods
Deque Interface Hierarchy Diagram
ArrayDeque implementation of Deque Interface Examples
Java ArrayDeque Example
import java.util.ArrayDeque;
import java.util.Deque;
public class ArrayDequeExample {
public static void main(String[] args) {
// Creating Deque and adding elements
Deque<String> deque = new ArrayDeque<String>();
deque.add("element1");
deque.add("element2");
deque.add("element3");
// Traversing elements
for (String str : deque) {
System.out.println(str);
}
}
}
Output:
element1
element2
element3
Java ArrayDeque Example: offerFirst() and pollLast()
package com.javaguides.collections.queueexamples;
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque <String> deque = new ArrayDeque < String > ();
deque.offer("element1");
deque.offer("element2");
deque.add("element3");
deque.offerFirst("element4");
System.out.println("After offerFirst Traversal...");
for (String s: deque) {
System.out.println(s);
}
// deque.poll();
// deque.pollFirst();//it is same as poll()
deque.pollLast();
System.out.println("After pollLast() Traversal...");
for (String s: deque) {
System.out.println(s);
}
}
}
Output:
After offerFirst Traversal...
element4
element1
element2
element3
After pollLast() Traversal...
element4
element1
element2
After offerFirst Traversal...
element4
element1
element2
element3
After pollLast() Traversal...
element4
element1
element2
Comments
Post a Comment
Leave Comment