-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_queue.java
42 lines (34 loc) · 1.26 KB
/
04_queue.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
import java.util.*;
class MyCollections {
public static void main(String args[]) {
example_queue();
}
public static void example_queue() {
// Create using
// Queue <Data Type> workingDaysInQueue = new Queue <Data Type> ();
Queue <String> workingDaysInQueue = new LinkedList<String> ();
workingDaysInQueue.add("Monday");
workingDaysInQueue.add("Tuesday");
workingDaysInQueue.add("Wednesday");
workingDaysInQueue.add("Thursday");
workingDaysInQueue.add("Friday");
// Size of the map
System.out.println("\nWorkday Map Size : " + workingDaysInQueue.size() );
// In a queue, elements are added at the end
workingDaysInQueue.add("Saturday");
// In a queue, elements are removed from the start
String item = workingDaysInQueue.remove();
System.out.println("Removed item {" + item + "} from the queue");
// Accessing elements in a queue
System.out.println("\nIterator : ");
Iterator itor = workingDaysInQueue.iterator();
while( itor.hasNext() ){
System.out.println("Element = " + itor.next() );
}
// Another way to access elements in a queue
System.out.println("\nforEach : ");
workingDaysInQueue.forEach( element -> {
System.out.println("Element = " + element );
});
}
}