Skip to content

Commit eaa8a75

Browse files
Neetika23Neetika Khandelwal
andauthored
BAEL-9192 (#18447)
* BAEL-9192 * BAEL-9192 * BAEL-9192 * BAEL-9192 --------- Co-authored-by: Neetika Khandelwal <[email protected]>
1 parent 3c10b94 commit eaa8a75

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<version>${junit.version}</version>
2626
<scope>test</scope>
2727
</dependency>
28+
<dependency>
29+
<groupId>com.github.stefanbirkner</groupId>
30+
<artifactId>system-lambda</artifactId>
31+
<version>${stefanbirkner.version}</version>
32+
<scope>test</scope>
33+
</dependency>
2834
</dependencies>
2935

3036
<build>
@@ -42,6 +48,7 @@
4248

4349
<properties>
4450
<junit.version>5.9.2</junit.version>
51+
<stefanbirkner.version>1.2.1</stefanbirkner.version>
4552
</properties>
4653

4754
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.ArrayList;
5+
import java.util.Comparator;
6+
import java.util.Deque;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.ListIterator;
10+
import java.util.Stack;
11+
12+
public class PrintStack {
13+
14+
public static void givenStack_whenUsingToString_thenPrintStack() {
15+
Stack<Integer> stack = new Stack<>();
16+
stack.push(10);
17+
stack.push(20);
18+
stack.push(30);
19+
System.out.print(stack.toString());
20+
}
21+
22+
public static void givenStack_whenUsingForEach_thenPrintStack() {
23+
Stack<Integer> stack = new Stack<>();
24+
stack.push(10);
25+
stack.push(20);
26+
stack.push(30);
27+
28+
List<Integer> result = new ArrayList<>();
29+
for (Integer value : stack) {
30+
System.out.print(value + " ");
31+
}
32+
}
33+
34+
public static void givenStack_whenUsingDirectForEach_thenPrintStack() {
35+
Stack<Integer> stack = new Stack<>();
36+
stack.push(10);
37+
stack.push(20);
38+
stack.push(30);
39+
40+
stack.forEach(element -> System.out.println(element));
41+
}
42+
43+
public static void givenStack_whenUsingStreamReverse_thenPrintStack() {
44+
Stack<Integer> stack = new Stack<>();
45+
stack.push(10);
46+
stack.push(20);
47+
stack.push(30);
48+
49+
stack.stream()
50+
.sorted(Comparator.reverseOrder())
51+
.forEach(System.out::println);
52+
}
53+
54+
public static void givenStack_whenUsingIterator_thenPrintStack() {
55+
Stack<Integer> stack = new Stack<>();
56+
stack.push(10);
57+
stack.push(20);
58+
stack.push(30);
59+
60+
Iterator<Integer> iterator = stack.iterator();
61+
while (iterator.hasNext()) {
62+
System.out.print(iterator.next() + " ");
63+
}
64+
}
65+
66+
public static void givenStack_whenUsingListIteratorReverseOrder_thenPrintStack() {
67+
Stack<Integer> stack = new Stack<>();
68+
stack.push(10);
69+
stack.push(20);
70+
stack.push(30);
71+
72+
ListIterator<Integer> iterator = stack.listIterator(stack.size());
73+
while (iterator.hasPrevious()) {
74+
System.out.print(iterator.previous() + " ");
75+
}
76+
}
77+
78+
public static void givenStack_whenUsingDeque_thenPrintStack() {
79+
Deque<Integer> stack = new ArrayDeque<>();
80+
stack.push(10);
81+
stack.push(20);
82+
stack.push(30);
83+
84+
stack.forEach(e -> System.out.print(e + " "));
85+
}
86+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.printstack;
2+
3+
import static com.github.stefanbirkner.systemlambda.SystemLambda.tapSystemOut;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
import com.baeldung.PrintStack;
9+
10+
class PrintStackUnitTest {
11+
12+
@Test
13+
void givenStack_whenUsingToString_thenPrintStack() throws Exception {
14+
String output = tapSystemOut(() -> PrintStack.givenStack_whenUsingToString_thenPrintStack());
15+
assertEquals("[10, 20, 30]", output);
16+
}
17+
18+
@Test
19+
void givenStack_whenUsingForEach_thenPrintStack() throws Exception {
20+
String output = tapSystemOut(() -> PrintStack.givenStack_whenUsingForEach_thenPrintStack());
21+
assertEquals("10 20 30 ", output);
22+
}
23+
24+
@Test
25+
void givenStack_whenUsingDirectForEach_thenPrintStack() throws Exception {
26+
String output = tapSystemOut(() -> PrintStack.givenStack_whenUsingDirectForEach_thenPrintStack());
27+
assertEquals("10\n20\n30\n", output.replace("\r\n", "\n"));
28+
}
29+
30+
@Test
31+
void givenStack_whenUsingStreamReverse_thenPrintStack() throws Exception {
32+
String output = tapSystemOut(() -> PrintStack.givenStack_whenUsingStreamReverse_thenPrintStack());
33+
assertEquals("30\n20\n10\n", output.replace("\r\n", "\n"));
34+
}
35+
36+
@Test
37+
void givenStack_whenUsingIterator_thenPrintStack() throws Exception {
38+
String output = tapSystemOut(() -> PrintStack.givenStack_whenUsingIterator_thenPrintStack());
39+
assertEquals("10 20 30 ", output);
40+
}
41+
42+
@Test
43+
void givenStack_whenUsingListIteratorReverseOrder_thenPrintStack() throws Exception {
44+
String output = tapSystemOut(() -> PrintStack.givenStack_whenUsingListIteratorReverseOrder_thenPrintStack());
45+
assertEquals("30 20 10 ", output);
46+
}
47+
48+
@Test
49+
void givenStack_whenUsingDeque_thenPrintStack() throws Exception {
50+
String output = tapSystemOut(() -> PrintStack.givenStack_whenUsingDeque_thenPrintStack());
51+
assertEquals("30 20 10 ", output);
52+
}
53+
}

0 commit comments

Comments
 (0)