The Stack
class represents a last-in-first-out (LIFO) stack of objects.
When a stack is first created, it contains no items.
A more complete and consistent set of LIFO stack operations is provided by the Deque
interface and its implementations, which should be used in preference to this class. For example:
Deque<Integer> stack = new ArrayDeque<Integer>(); // ArrayDeque since Java 1.6
If we want only stack behavior, inheritance is inappropriate in java because that would produce a class with all the rest of the ArrayDeque methods. Using composition, we choose which methods to expose and how to name them.
java.util.Stack is a bad desgin. It is extended java.util.Vector<E>.
case 1:
// onjava/Stack.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// A Stack class built with an ArrayDeque
package onjava;
import java.util.ArrayDeque;
import java.util.Deque;
public class Stack<T> {
private Deque<T> storage = new ArrayDeque<>();
public void push(T v) {
storage.push(v);
}
public T peek() {
return storage.peek();
}
public T pop() {
return storage.pop();
}
public boolean isEmpty() {
return storage.isEmpty();
}
@Override
public String toString() {
return storage.toString();
}
}
The peek() method provides us with the top element without removing it from the top of the stack, while pop() removes and returns the top element.
case 2:
// collections/StackTest2.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
import onjava.*;
public class StackTest2 {
public static void main(String[] args) {
Stack<String> stack = new Stack<>(); // use case 1 class Stack
for (String s : "My dog has fleas".split(" ")) {
stack.push(s);
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
}
}
/* Output:
fleas has dog My
*/
case 3:
// collections/StackCollision.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
public class StackCollision {
public static void main(String[] args) {
onjava.Stack<String> stack = new onjava.Stack<>();
for (String s : "My dog has fleas".split(" ")) {
stack.push(s);
}
while (!stack.isEmpty()) {
System.out.print(stack.pop() + " ");
}
System.out.println();
java.util.Stack<String> stack2 = new java.util.Stack<>();
for (String s : "My dog has fleas".split(" ")) {
stack2.push(s);
}
while (!stack2.empty()) {
System.out.print(stack2.pop() + " ");
}
}
}
/* Output:
fleas has dog My
fleas has dog My
*/
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/onjava/Stack.java
3. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/StackTest2.java
4. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/collections/StackCollision.java
5. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Stack.java
6. https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html