Skip to content

Commit 355ce20

Browse files
authored
Merge pull request #186 from javaevolved/brunoborges-pattern-stack-to-deque-arraydeque
Add Stack to Deque and ArrayDeque pattern
2 parents 118e72d + 1a24afd commit 355ce20

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
id: 5ec6dc04-e96e-4d30-a11c-ea48c9d28fbf
3+
slug: "stack-to-deque"
4+
title: "Stack to Deque and ArrayDeque"
5+
category: "collections"
6+
difficulty: "beginner"
7+
jdkVersion: "6"
8+
oldLabel: "Legacy Stack"
9+
modernLabel: "Deque API"
10+
oldApproach: "Stack"
11+
modernApproach: "Deque with ArrayDeque"
12+
oldCode: |-
13+
Stack<String> stack = new Stack<>();
14+
stack.push("task");
15+
String next = stack.pop();
16+
modernCode: |-
17+
Deque<String> stack = new ArrayDeque<>();
18+
stack.push("task");
19+
String next = stack.pop();
20+
summary: "Use the Deque interface with ArrayDeque instead of the legacy Stack class."
21+
explanation: "Stack extends the legacy synchronized Vector class. Deque models stack operations directly, and ArrayDeque is the preferred general-purpose implementation for in-memory LIFO work."
22+
whyModernWins:
23+
- icon: "🎯"
24+
title: "Better abstraction"
25+
desc: "Deque explicitly models both stack and queue operations."
26+
- icon: ""
27+
title: "Lower overhead"
28+
desc: "ArrayDeque avoids Vector's legacy synchronization."
29+
- icon: "🧩"
30+
title: "More flexible"
31+
desc: "The same interface supports LIFO and FIFO algorithms."
32+
support:
33+
state: "available"
34+
description: "Available since JDK 6 (December 2006)"
35+
prev: "enterprise/spring-boot-mvc-config"
36+
next: null
37+
related:
38+
- "collections/sequenced-collections"
39+
- "collections/reverse-list-iteration"
40+
- "collections/immutable-list-creation"
41+
tags:
42+
- collections
43+
docs:
44+
- title: "Deque (Java 25)"
45+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Deque.html"
46+
- title: "ArrayDeque (Java 25)"
47+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/ArrayDeque.html"
48+
- title: "Stack (Java 25)"
49+
href: "https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/util/Stack.html"

content/enterprise/spring-boot-mvc-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ support:
4444
state: "available"
4545
description: "Available in Spring Boot 4.0 with Spring Framework 7.0 (requires Java 17+)"
4646
prev: "enterprise/spring-null-safety-jspecify"
47-
next: null
47+
next: "collections/stack-to-deque"
4848
related:
4949
- "enterprise/spring-api-versioning"
5050
- "enterprise/spring-xml-config-vs-annotations"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
///usr/bin/env jbang "$0" "$@" ; exit $?
2+
//JAVA 25+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
/// Proof: stack-to-deque
7+
/// Source: content/collections/stack-to-deque.yaml
8+
void main() {
9+
Deque<String> stack = new ArrayDeque<>();
10+
stack.push("task");
11+
String next = stack.pop();
12+
}

0 commit comments

Comments
 (0)