A Deque (Double Ended Queue) in Java is a linear data structure that allows the insertion and deletion of elements from both the front and the rear ends. It can be used as a queue or a stack and enables efficient access to both ends of the collection.
import java.util.ArrayDeque;
public class DequeExample {
public static void main(String[] args) {
ArrayDeque<Integer> deque = new ArrayDeque<>();
// Adding elements to the front and rear
deque.addFirst(1);
deque.addLast(2);
// Displaying elements
System.out.println("Deque: " + deque);
// Removing elements from the front and rear
int front = deque.removeFirst();
int rear = deque.removeLast();
System.out.println("Removed from front: " + front);
System.out.println("Removed from rear: " + rear);
System.out.println("Deque after removals: " + deque);
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?