-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_stack.java
45 lines (36 loc) · 1.31 KB
/
05_stack.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
class MyCollections {
public static void main(String args[]) {
example_stack();
}
public static void example_stack() {
// LIFO = Last In, Fast Out
// Create using
// Stack <Data Type> variable = new Stack <String> ();
Stack <String> workingDaysInStack = new Stack <String> ();
// Items are added on top
workingDaysInStack.push("Monday");
workingDaysInStack.push("Tuesday");
workingDaysInStack.push("Wednesday");
workingDaysInStack.push("Thursday");
workingDaysInStack.push("Friday");
// Size of the map
System.out.println("\nWorkday Stack Size : " + workingDaysInStack.size() );
// In a Stack, elements are added at the top
workingDaysInStack.push("Saturday");
// In a Stack, elements are removed from the top
String item = workingDaysInStack.pop();
System.out.println("Removed item {" + item + "} from the queue");
// Accessing elements in a queue
System.out.println("\nIterator : ");
Iterator itor = workingDaysInStack.iterator();
while( itor.hasNext() ){
System.out.println("Element = " + itor.next() );
}
// Another way to access elements in a queue
System.out.println("\nforEach : ");
workingDaysInStack.forEach( element -> {
System.out.println("Element = " + element );
});
}
}