Learnitweb

Dequeue in Java

The Deque (Double-Ended Queue) interface in Java is part of the java.util package and provides a more flexible and efficient way to handle collections. It supports the insertion and removal of elements from both ends of the queue, which makes it versatile for implementing both FIFO (First-In-First-Out) and LIFO (Last-In-First-Out) behaviors.

Key Methods of Deque

  • addFirst(E e): Adds the specified element to the front of the deque.
Deque<Integer> deque = new ArrayDeque<>();
deque.addFirst(1);
System.out.println(deque); // Output: [1]
    • addLast(E e): Adds the specified element to the end of the deque.
    deque.addLast(2);
    System.out.println(deque); // Output: [1, 2]
    • offerFirst(E e): Adds the specified element to the front of the deque, returning true upon success.
    deque.offerFirst(0);
    System.out.println(deque); // Output: [0, 1, 2]
    • offerLast(E e): Adds the specified element to the end of the deque, returning true upon success.
    deque.offerLast(3);
    System.out.println(deque); // Output: [0, 1, 2, 3]
    • removeFirst(): Removes and returns the first element from the deque.
    int first = deque.removeFirst();
    System.out.println(first);  // Output: 0
    System.out.println(deque);  // Output: [1, 2, 3]
    • removeLast(): Removes and returns the last element from the deque.
    int last = deque.removeLast();
    System.out.println(last);   // Output: 3
    System.out.println(deque);  // Output: [1, 2]
    • pollFirst(): Retrieves and removes the first element from the deque, or returns null if the deque is empty.
    int pollFirst = deque.pollFirst();
    System.out.println(pollFirst);  // Output: 1
    System.out.println(deque);     // Output: [2]
    • pollLast(): Retrieves and removes the last element from the deque, or returns null if the deque is empty.
    int pollLast = deque.pollLast();
    System.out.println(pollLast);  // Output: 2
    System.out.println(deque);     // Output: []
    
    • getFirst(): Retrieves, but does not remove, the first element of the deque.
    deque.addFirst(10);
    System.out.println(deque.getFirst());  // Output: 10
    • getLast(): Retrieves, but does not remove, the last element of the deque.
    deque.addLast(20);
    System.out.println(deque.getLast());  // Output: 20

    Why Use Deque Over Stack?

    1. Greater Flexibility: Deque supports both FIFO and LIFO operations, unlike Stack, which is strictly LIFO.
    2. Better Performance: Deque implementations like ArrayDeque offer more efficient operations, providing O(1) time complexity for adding/removing elements from both ends.
    3. No Synchronization Overhead: Stack is synchronized, which can reduce performance in single-threaded scenarios. Deque is not synchronized by default, offering better performance and flexibility.
    4. Modern API: Deque offers more intuitive and useful methods like offerFirst(), offerLast(), and pollFirst() that provide better control over data management.
    5. Integration with Collections Framework: Deque is a modern part of the Java Collections Framework and adheres to current Java design principles, unlike the legacy Stack.

    Conclusion

    In Java, Deque is a more flexible, efficient, and modern alternative to the legacy Stack class. It provides both FIFO and LIFO support with better performance and cleaner API methods. If you need a stack-like structure, using Deque is generally a better choice.