Skip to content

Commit ce8e9d2

Browse files
committed
deque vs. stack article
1 parent f0816fe commit ce8e9d2

4 files changed

Lines changed: 194 additions & 1 deletion

File tree

core-java-modules/core-java-collections-4/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</dependencies>
2626

2727
<properties>
28-
<assertj.version>3.18.0</assertj.version>
28+
<assertj.version>3.19.0</assertj.version>
2929
</properties>
3030

3131
</project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.baeldung.collections.dequestack;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Collection;
5+
import java.util.Deque;
6+
import java.util.Iterator;
7+
8+
public class ArrayLifoStack<E> implements LifoStack<E> {
9+
private final Deque<E> deque = new ArrayDeque<>();
10+
11+
@Override
12+
public void push(E item) {
13+
deque.addFirst(item);
14+
}
15+
16+
@Override
17+
public E pop() {
18+
return deque.removeFirst();
19+
}
20+
21+
@Override
22+
public E peek() {
23+
return deque.peekFirst();
24+
}
25+
26+
// implementing methods from the Collection interface
27+
@Override
28+
public int size() {
29+
return deque.size();
30+
}
31+
32+
@Override
33+
public boolean isEmpty() {
34+
return deque.isEmpty();
35+
}
36+
37+
@Override
38+
public boolean contains(Object o) {
39+
return deque.contains(o);
40+
}
41+
42+
@Override
43+
public Iterator<E> iterator() {
44+
return deque.iterator();
45+
}
46+
47+
@Override
48+
public Object[] toArray() {
49+
return deque.toArray();
50+
}
51+
52+
@Override
53+
public <T> T[] toArray(T[] a) {
54+
return deque.toArray(a);
55+
}
56+
57+
@Override
58+
public boolean add(E e) {
59+
return deque.add(e);
60+
}
61+
62+
@Override
63+
public boolean remove(Object o) {
64+
return deque.remove(o);
65+
}
66+
67+
@Override
68+
public boolean containsAll(Collection<?> c) {
69+
return deque.containsAll(c);
70+
}
71+
72+
@Override
73+
public boolean addAll(Collection<? extends E> c) {
74+
return deque.addAll(c);
75+
}
76+
77+
@Override
78+
public boolean removeAll(Collection<?> c) {
79+
return deque.removeAll(c);
80+
}
81+
82+
@Override
83+
public boolean retainAll(Collection<?> c) {
84+
return deque.retainAll(c);
85+
}
86+
87+
@Override
88+
public void clear() {
89+
deque.clear();
90+
}
91+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.collections.dequestack;
2+
3+
import java.util.Collection;
4+
5+
public interface LifoStack<E> extends Collection<E> {
6+
7+
E peek();
8+
9+
E pop();
10+
11+
void push(E item);
12+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.baeldung.collections.dequestack;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.ArrayDeque;
6+
import java.util.Deque;
7+
import java.util.Iterator;
8+
import java.util.Stack;
9+
10+
import static org.assertj.core.api.Assertions.assertThat;
11+
12+
class StackVsDequeUnitTest {
13+
14+
@Test
15+
void givenAStack_whenAccessByIndex_thenElementCanBeRead() {
16+
Stack<String> myStack = new Stack<>();
17+
myStack.push("I am the 1st element."); //index 0
18+
myStack.push("I am the 2nd element."); //index 1
19+
myStack.push("I am the 3rd element."); //index 2
20+
//access by index
21+
assertThat(myStack.get(0)).isEqualTo("I am the 1st element.");
22+
}
23+
24+
@Test
25+
void givenAStack_whenIterate_thenFromBottomToTop() {
26+
Stack<String> myStack = new Stack<>();
27+
myStack.push("I am at the bottom.");
28+
myStack.push("I am in the middle.");
29+
myStack.push("I am at the top.");
30+
31+
Iterator<String> it = myStack.iterator();
32+
33+
assertThat(it).toIterable().containsExactly(
34+
"I am at the bottom.",
35+
"I am in the middle.",
36+
"I am at the top.");
37+
}
38+
39+
@Test
40+
void givenAStack_whenAddOrRemoveByIndex_thenElementCanBeAddedOrRemoved() {
41+
Stack<String> myStack = new Stack<>();
42+
myStack.push("I am the 1st element.");
43+
myStack.push("I am the 3rd element.");
44+
45+
assertThat(myStack.size()).isEqualTo(2);
46+
47+
//insert by index
48+
myStack.add(1, "I am the 2nd element.");
49+
assertThat(myStack.size()).isEqualTo(3);
50+
assertThat(myStack.get(1)).isEqualTo("I am the 2nd element.");
51+
//remove by index
52+
myStack.remove(1);
53+
assertThat(myStack.size()).isEqualTo(2);
54+
}
55+
56+
@Test
57+
void givenADeque_whenAddOrRemoveLastElement_thenTheLastElementCanBeAddedOrRemoved() {
58+
Deque<String> myStack = new ArrayDeque<>();
59+
myStack.push("I am the 1st element.");
60+
myStack.push("I am the 2nd element.");
61+
myStack.push("I am the 3rd element.");
62+
63+
assertThat(myStack.size()).isEqualTo(3);
64+
65+
//insert element to the bottom of the stack
66+
myStack.addLast("I am the NEW element.");
67+
assertThat(myStack.size()).isEqualTo(4);
68+
assertThat(myStack.peek()).isEqualTo("I am the 3rd element.");
69+
70+
//remove element from the bottom of the stack
71+
String removedStr = myStack.removeLast();
72+
assertThat(myStack.size()).isEqualTo(3);
73+
assertThat(removedStr).isEqualTo("I am the NEW element.");
74+
}
75+
76+
@Test
77+
void givenADeque_whenIterate_thenFromTopToBottom() {
78+
Deque<String> myStack = new ArrayDeque<>();
79+
myStack.push("I am at the bottom.");
80+
myStack.push("I am in the middle.");
81+
myStack.push("I am at the top.");
82+
83+
Iterator<String> it = myStack.iterator();
84+
85+
assertThat(it).toIterable().containsExactly(
86+
"I am at the top.",
87+
"I am in the middle.",
88+
"I am at the bottom.");
89+
}
90+
}

0 commit comments

Comments
 (0)