🎓 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
Introduction
The ArrayDeque class in Java, part of the java.util package, is a resizable array implementation of the Deque interface. It provides a double-ended queue, allowing elements to be added or removed from both ends.
Table of Contents
- What is the
ArrayDequeClass? - Features of
ArrayDeque - Common Methods
- Examples of Using
ArrayDeque - Conclusion
1. What is the ArrayDeque Class?
ArrayDeque is a collection that supports element insertion and removal at both ends, offering an efficient alternative to linked lists and other deque implementations.
2. Features of ArrayDeque
- No capacity restrictions (dynamically resizable).
- Faster than
LinkedListfor queue operations. - Not thread-safe (requires external synchronization for concurrent use).
3. Common Methods
addFirst(E e): Inserts the specified element at the front.addLast(E e): Inserts the specified element at the end.offerFirst(E e): Inserts the specified element at the front without throwing an exception.offerLast(E e): Inserts the specified element at the end without throwing an exception.removeFirst(): Removes and returns the first element.removeLast(): Removes and returns the last element.pollFirst(): Retrieves and removes the first element, or returnsnullif empty.pollLast(): Retrieves and removes the last element, or returnsnullif empty.getFirst(): Retrieves, but does not remove, the first element.getLast(): Retrieves, but does not remove, the last element.peekFirst(): Retrieves, but does not remove, the first element, or returnsnull.peekLast(): Retrieves, but does not remove, the last element, or returnsnull.size(): Returns the number of elements in the deque.
4. Examples of Using ArrayDeque
Example 1: Basic Operations
This example demonstrates basic operations such as adding, removing, and accessing elements.
import java.util.ArrayDeque;
public class ArrayDequeExample {
public static void main(String[] args) {
ArrayDeque<String> deque = new ArrayDeque<>();
// Adding elements
deque.addFirst("First");
deque.addLast("Last");
System.out.println("Deque after additions: " + deque);
// Accessing elements
System.out.println("First element: " + deque.getFirst());
System.out.println("Last element: " + deque.getLast());
// Removing elements
deque.removeFirst();
deque.removeLast();
System.out.println("Deque after removals: " + deque);
}
}
Output:
Deque after additions: [First, Last]
First element: First
Last element: Last
Deque after removals: []
Example 2: Using offer and poll
This example shows the use of offer and poll methods for safe element insertion and removal.
import java.util.ArrayDeque;
public class OfferPollExample {
public static void main(String[] args) {
ArrayDeque<Integer> deque = new ArrayDeque<>();
// Offering elements
deque.offerFirst(1);
deque.offerLast(2);
System.out.println("Deque after offers: " + deque);
// Polling elements
System.out.println("Polled first: " + deque.pollFirst());
System.out.println("Polled last: " + deque.pollLast());
System.out.println("Deque after polls: " + deque);
}
}
Output:
Deque after offers: [1, 2]
Polled first: 1
Polled last: 2
Deque after polls: []
Example 3: Peeking at Elements
This example demonstrates how to peek at elements without removing them.
import java.util.ArrayDeque;
public class PeekExample {
public static void main(String[] args) {
ArrayDeque<String> deque = new ArrayDeque<>();
deque.add("Element");
System.out.println("Peek first: " + deque.peekFirst());
System.out.println("Peek last: " + deque.peekLast());
}
}
Output:
Peek first: Element
Peek last: Element
5. Conclusion
The ArrayDeque class in Java is a versatile and efficient implementation of the Deque interface. It offers fast operations for both ends of the queue, making it a suitable choice for many applications that require double-ended queue functionality.
My 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
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment