🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
element2Reference
https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Deque.htmlMy Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business


Comments
Post a Comment
Leave Comment