Merge pull request #10511 from sk1418/deque_stack
deque vs. stack article
This commit is contained in:
commit
b40ae6e80b
|
@ -25,7 +25,7 @@
|
|||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.18.0</assertj.version>
|
||||
<assertj.version>3.19.0</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,91 @@
|
|||
package com.baeldung.collections.dequestack;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collection;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class ArrayLifoStack<E> implements LifoStack<E> {
|
||||
private final Deque<E> deque = new ArrayDeque<>();
|
||||
|
||||
@Override
|
||||
public void push(E item) {
|
||||
deque.addFirst(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() {
|
||||
return deque.removeFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E peek() {
|
||||
return deque.peekFirst();
|
||||
}
|
||||
|
||||
// implementing methods from the Collection interface
|
||||
@Override
|
||||
public int size() {
|
||||
return deque.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return deque.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(Object o) {
|
||||
return deque.contains(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return deque.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toArray() {
|
||||
return deque.toArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T[] toArray(T[] a) {
|
||||
return deque.toArray(a);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(E e) {
|
||||
return deque.add(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(Object o) {
|
||||
return deque.remove(o);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean containsAll(Collection<?> c) {
|
||||
return deque.containsAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(Collection<? extends E> c) {
|
||||
return deque.addAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(Collection<?> c) {
|
||||
return deque.removeAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(Collection<?> c) {
|
||||
return deque.retainAll(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
deque.clear();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.collections.dequestack;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
public interface LifoStack<E> extends Collection<E> {
|
||||
|
||||
E peek();
|
||||
|
||||
E pop();
|
||||
|
||||
void push(E item);
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
package com.baeldung.collections.dequestack;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
import java.util.Stack;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class StackVsDequeUnitTest {
|
||||
|
||||
@Test
|
||||
void givenAStack_whenAccessByIndex_thenElementCanBeRead() {
|
||||
Stack<String> myStack = new Stack<>();
|
||||
myStack.push("I am the 1st element."); //index 0
|
||||
myStack.push("I am the 2nd element."); //index 1
|
||||
myStack.push("I am the 3rd element."); //index 2
|
||||
//access by index
|
||||
assertThat(myStack.get(0)).isEqualTo("I am the 1st element.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAStack_whenIterate_thenFromBottomToTop() {
|
||||
Stack<String> myStack = new Stack<>();
|
||||
myStack.push("I am at the bottom.");
|
||||
myStack.push("I am in the middle.");
|
||||
myStack.push("I am at the top.");
|
||||
|
||||
Iterator<String> it = myStack.iterator();
|
||||
|
||||
assertThat(it).toIterable().containsExactly(
|
||||
"I am at the bottom.",
|
||||
"I am in the middle.",
|
||||
"I am at the top.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAStack_whenAddOrRemoveByIndex_thenElementCanBeAddedOrRemoved() {
|
||||
Stack<String> myStack = new Stack<>();
|
||||
myStack.push("I am the 1st element.");
|
||||
myStack.push("I am the 3rd element.");
|
||||
|
||||
assertThat(myStack.size()).isEqualTo(2);
|
||||
|
||||
//insert by index
|
||||
myStack.add(1, "I am the 2nd element.");
|
||||
assertThat(myStack.size()).isEqualTo(3);
|
||||
assertThat(myStack.get(1)).isEqualTo("I am the 2nd element.");
|
||||
//remove by index
|
||||
myStack.remove(1);
|
||||
assertThat(myStack.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenADeque_whenAddOrRemoveLastElement_thenTheLastElementCanBeAddedOrRemoved() {
|
||||
Deque<String> myStack = new ArrayDeque<>();
|
||||
myStack.push("I am the 1st element.");
|
||||
myStack.push("I am the 2nd element.");
|
||||
myStack.push("I am the 3rd element.");
|
||||
|
||||
assertThat(myStack.size()).isEqualTo(3);
|
||||
|
||||
//insert element to the bottom of the stack
|
||||
myStack.addLast("I am the NEW element.");
|
||||
assertThat(myStack.size()).isEqualTo(4);
|
||||
assertThat(myStack.peek()).isEqualTo("I am the 3rd element.");
|
||||
|
||||
//remove element from the bottom of the stack
|
||||
String removedStr = myStack.removeLast();
|
||||
assertThat(myStack.size()).isEqualTo(3);
|
||||
assertThat(removedStr).isEqualTo("I am the NEW element.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenADeque_whenIterate_thenFromTopToBottom() {
|
||||
Deque<String> myStack = new ArrayDeque<>();
|
||||
myStack.push("I am at the bottom.");
|
||||
myStack.push("I am in the middle.");
|
||||
myStack.push("I am at the top.");
|
||||
|
||||
Iterator<String> it = myStack.iterator();
|
||||
|
||||
assertThat(it).toIterable().containsExactly(
|
||||
"I am at the top.",
|
||||
"I am in the middle.",
|
||||
"I am at the bottom.");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue