Queue

The Queue interface in Java represents a collection of elements where the order of elements is crucial. It follows the First-In-First-Out (FIFO) principle, meaning that the element that is added first is the one that is removed first. The Queue interface extends the Collection interface and is part of the java.util package.

Example:

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();

        // Adding elements to the queue
        queue.offer("Element 1");
        queue.offer("Element 2");
        queue.offer("Element 3");

        // Removing elements from the queue
        String removedElement = queue.poll();
        System.out.println("Removed Element: " + removedElement);

        // Peeking at the front element
        String frontElement = queue.peek();
        System.out.println("Front Element: " + frontElement);
    }
}

2. Deque Interface

The Deque interface (Double Ended Queue) extends the Queue interface and allows elements to be added or removed from both ends. It can function as both a queue and a stack. The Deque interface is also part of the java.util package.

Example:

import java.util.ArrayDeque;
import java.util.Deque;

public class DequeExample {
    public static void main(String[] args) {
        Deque<String> deque = new ArrayDeque<>();

        // Adding elements to the front
        deque.addFirst("Element 1");
        deque.addFirst("Element 2");

        // Adding elements to the end
        deque.addLast("Element 3");

        // Removing elements from the front and end
        String removedFromFront = deque.removeFirst();
        String removedFromEnd = deque.removeLast();

        System.out.println("Removed from Front: " + removedFromFront);
        System.out.println("Removed from End: " + removedFromEnd);
    }
}

Benchmark Performance Results:

Performance benchmarks for Queue and Deque operations depend on the specific implementation used (e.g., LinkedList for Queue, ArrayDeque for Deque). In general, operations such as adding and removing elements have constant time complexity.

Brief Comparison Table:

FeatureQueueDeque

Order

FIFO (First-In-First-Out)

Both FIFO and LIFO

Implementation

Usually LinkedList

Usually ArrayDeque

Methods

offer(), poll(), peek()

addFirst(), addLast(), removeFirst(), removeLast(), peekFirst(), peekLast()

Summary:

  • Use Queue when you need a simple FIFO structure.

  • Use Deque when you need both FIFO and LIFO operations or need efficient insertion and removal at both ends.

  • The choice between implementations (LinkedList or ArrayDeque) may depend on specific use cases and performance requirements.

Choose Queue for simple FIFO scenarios, and use Deque when you need more flexibility or efficiency in handling elements at both ends. The specific implementation may affect performance, so benchmarking in the context of your application is recommended.

Last updated