diff --git a/content/java/concepts/deque/deque.md b/content/java/concepts/deque/deque.md index 36a46f9f00c..fb0cda20177 100644 --- a/content/java/concepts/deque/deque.md +++ b/content/java/concepts/deque/deque.md @@ -1,22 +1,22 @@ --- 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; @@ -24,11 +24,57 @@ import java.util.deque; Deque myDeque = new DequeClass(); ``` -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 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; @@ -37,10 +83,12 @@ import java.util.ArrayDeque; public class Main { public static void main(String[] args) { Deque food = new ArrayDeque(); + 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()); @@ -49,7 +97,7 @@ public class Main { } ``` -The above example results in the following output: +Here is the output: ```shell [Potatoes, Cabbage, Sausage, Salad] @@ -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 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.