|
| 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" |
0 commit comments