Skip to content

[Edit] Java: Deque #7447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 108 additions & 29 deletions content/java/concepts/deque/deque.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,80 @@
---
Title: 'Deque'
Description: 'A Deque extends the Queue interface and offers additional methods to access and manipulate items at the top and end of the deque.'
Description: 'The Deque interface extends the Queue interface and provides methods to access and manipulate items at the top and end of a deque.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Queues'
- 'Stacks'
- 'Collections'
- 'Interface'
- 'Queues'
- 'Stacks'
CatalogContent:
- 'learn-java'
- 'paths/computer-science'
---

A **Deque** extends the [`Queue`](https://www.codecademy.com/resources/docs/java/queue) interface and is a double-ended queue. It provides methods to add, access, and remove items at the top and end of the deque. Thereby, it can be used as a queue or stack. The `Deque` interface is included in the `java.util` package.
The Java **`Deque`** interface extends the [`Queue`](https://www.codecademy.com/resources/docs/java/queue) interface and is a double-ended queue. It provides methods to add, access, and remove items at the top and end of a deque. Thereby, it can be used as a queue or stack. The `Deque` interface is included in the `java.util` package.

## Syntax
## Java `Deque` Syntax

```pseudo
import java.util.deque;

Deque<DataType> myDeque = new DequeClass<DataType>();
```

Where [`DataType`](https://www.codecademy.com/resources/docs/java/data-types) is the data type to be stored in the deque, and `DequeClass` is a class implementing the `Deque` interface, for example, `ArrayDeque` or `LinkedList`.
In the syntax:

- `DataType`: The [data type](https://www.codecademy.com/resources/docs/java/data-types) to be stored in the deque.
- `DequeClass`: A class implementing the `Deque` interface, e.g., `ArrayDeque` or `LinkedList`.

## Example
## Java `Deque` Methods

The following is an example of the `Deque` interface implemented by an `ArrayDeque`:
This list contains a selection of methods provided by the Java `Deque` interface:

- `.addFirst(item)`: Adds `item` at the top of the `Deque` if possible, otherwise it throws an exception.
- `.addLast(item)`: Adds `item` at the end of the `Deque` if possible, otherwise it throws an exception.
- `.getFirst()`: Returns, without removal, the first item of the `Deque`. It throws an exception if the Deque is empty.
- `.offerLast(item)`: Adds `item` at the end of the `Deque` if possible, otherwise it returns `false`.
- `.peekFirst()`: Returns, without removal, the first element of the `Deque`. Returns `null` if the `Deque` is empty.
- `.pollFirst()`: Returns and removes the first item of the `Deque`. Returns `null` if the `Deque` is empty.
- `.removeFirst()`: Returns and removes the first item of the `Deque`. Throws an exception if the `Deque` is empty.

## Example 1: Implementing Java `Deque` Using `LinkedList`

This example shows how to implement Java `Deque` using `LinkedList`:

```java
import java.util.Deque;
import java.util.LinkedList;

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

deque.addFirst("Apple");
deque.addLast("Banana");
deque.addFirst("Orange");

System.out.println("Deque: " + deque);

deque.removeLast();
System.out.println("After removing last: " + deque);
}
}
```

Here is the output:

```shell
Deque: [Orange, Apple, Banana]
After removing last: [Orange, Apple]
```

## Example 2: Implementing Java `Deque` Using `ArrayDeque`

This example shows how to implement Java `Deque` using `ArrayDeque`:

```java
import java.util.Deque;
Expand All @@ -37,10 +83,12 @@ import java.util.ArrayDeque;
public class Main {
public static void main(String[] args) {
Deque<String> food = new ArrayDeque<String>();

food.addFirst("Cabbage");
food.addLast("Sausage");
food.addFirst("Potatoes");
food.addLast("Salad");

System.out.println(food);
System.out.println(food.pollFirst());
System.out.println(food.peekFirst());
Expand All @@ -49,7 +97,7 @@ public class Main {
}
```

The above example results in the following output:
Here is the output:

```shell
[Potatoes, Cabbage, Sausage, Salad]
Expand All @@ -58,28 +106,59 @@ Cabbage
[Cabbage, Sausage, Salad]
```

## Methods
## Example 3: Using Java `Deque` as a Stack

The following list contains a selection of methods provided by the `Deque` interface:
This example shows how to use Java `Deque` as a stack:

- `.addFirst(item)`: Adds `item` at the top of the `Deque` if possible, otherwise it throws an exception.
- `.addLast(item)`: Adds `item` at the end of the `Deque` if possible, otherwise it throws an exception.
- `.getFirst()`: Returns, without removal, the first item of the `Deque`. It throws an exception if the Deque is empty.
- `.offerLast(item)`: Adds `item` at the end of the `Deque` if possible, otherwise it returns `false`.
- `.peekFirst()`: Returns, without removal, the first element of the `Deque`. Returns `null` if the `Deque` is empty.
- `.pollFirst()`: Returns and removes the first item of the `Deque`. Returns `null` if the `Deque` is empty.
- `.removeFirst()`: Returns and removes the first item of the `Deque`. Throws an exception if the `Deque` is empty.
```java
import java.util.Deque;
import java.util.ArrayDeque;

public class StackExample {
public static void main(String[] args) {
Deque<Integer> stack = new ArrayDeque<>();

stack.push(10);
stack.push(20);
stack.push(30);

System.out.println("Stack: " + stack);

System.out.println("Popped: " + stack.pop());
System.out.println("After pop: " + stack);
}
}
```

Here is the output:

```shell
Stack: [30, 20, 10]
Popped: 30
After pop: [20, 10]
```

## Frequently Asked Questions

### 1. What is the difference between Java `Deque` and `Queue`?

A Java `Deque` allows insertion and deletion at both ends, whereas a `Queue` allows insertion at the tail and deletion at the head.

### 2. Which classes implement the Java `Deque` interface?

These classes implement the Java `Deque` interface:

- `LinkedList`
- `ArrayDeque`

### 3. Can a Java `Deque` contain null elements?

Some implementations of a Java `Deque` like `LinkedList` allow null, but `ArrayDeque` does not permit null elements.

### 4. Is Java `Deque` thread-safe?

### Equivalent methods Stack and Queue
No, Java `Deque` implementations like `ArrayDeque` and `LinkedList` are not thread-safe. Use `ConcurrentLinkedDeque` for thread-safe operations.

The following table lists the equivalent methods for `Queue` and `Stack`:
### 5. When should I use a Java `Deque` instead of a `Stack` class?

| `Deque` method | Equivalent `Queue` method | Equivalent `Stack` method |
| :----------------: | :-----------------------: | :-----------------------: |
| `.addFirst(item)` | - | `.push(item)` |
| `.addLast(item)` | `.add(item)` | - |
| `.getFirst()` | `.element()` | - |
| `.offerLast(item)` | `.offer(item)` | - |
| `.peekFirst()` | `.peek()` | `.peek()` |
| `.pollFirst()` | `.poll()` | - |
| `.removeFirst()` | `.remove()` | `.pop()` |
Java `Deque` is preferred over `Stack` because `Stack` is synchronized and slower for single-threaded use. `Deque` offers better performance and flexibility.