# 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:

```java
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:

```java
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:

<table data-full-width="false"><thead><tr><th>Feature</th><th>Queue</th><th>Deque</th></tr></thead><tbody><tr><td>Order</td><td>FIFO (First-In-First-Out)</td><td>Both FIFO and LIFO</td></tr><tr><td>Implementation</td><td>Usually LinkedList</td><td>Usually ArrayDeque</td></tr><tr><td>Methods</td><td><code>offer()</code>, <code>poll()</code>, <code>peek()</code></td><td><code>addFirst()</code>, <code>addLast()</code>, <code>removeFirst()</code>, <code>removeLast()</code>, <code>peekFirst()</code>, <code>peekLast()</code></td></tr></tbody></table>

### 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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://codewithmeiy.gitbook.io/core-java/collections-framework/queue.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
