-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjava-queue-example
More file actions
112 lines (87 loc) · 4.39 KB
/
java-queue-example
File metadata and controls
112 lines (87 loc) · 4.39 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class IterateOverQueueExample {
public static void main(String[] args) {
Queue<String> waitingQueue = new LinkedList<>();
waitingQueue.add("John");
waitingQueue.add("Brad");
waitingQueue.add("Angelina");
waitingQueue.add("Julia");
System.out.println("=== Iterating over a Queue using Java 8 forEach() ===");
waitingQueue.forEach(name -> {
System.out.println(name);
});
System.out.println("\n=== Iterating over a Queue using iterator() ===");
Iterator<String> waitingQueueIterator = waitingQueue.iterator();
while (waitingQueueIterator.hasNext()) {
String name = waitingQueueIterator.next();
System.out.println(name);
}
System.out.println("\n=== Iterating over a Queue using iterator() and Java 8 forEachRemaining() ===");
waitingQueueIterator = waitingQueue.iterator();
waitingQueueIterator.forEachRemaining(name -> {
System.out.println(name);
});
System.out.println("\n=== Iterating over a Queue using simple for-each loop ===");
for(String name: waitingQueue) {
System.out.println(name);
}
}
}
************************************************************************************************************************************************************************
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
// Create and initialize a Queue using a LinkedList
Queue<String> waitingQueue = new LinkedList<>();
// Adding new elements to the Queue (The Enqueue operation)
waitingQueue.add("Rajeev");
waitingQueue.add("Chris");
waitingQueue.add("John");
waitingQueue.add("Mark");
waitingQueue.add("Steven");
System.out.println("WaitingQueue : " + waitingQueue);
// Removing an element from the Queue using remove() (The Dequeue operation)
// The remove() method throws NoSuchElementException if the Queue is empty
String name = waitingQueue.remove();
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
// Removing an element from the Queue using poll()
// The poll() method is similar to remove() except that it returns null if the Queue is empty.
name = waitingQueue.poll();
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
}
}
***********************************************************************************************************************************************************************
import java.util.LinkedList;
import java.util.Queue;
public class QueueSizeSearchFrontExample {
public static void main(String[] args) {
Queue<String> waitingQueue = new LinkedList<>();
waitingQueue.add("Jennifer");
waitingQueue.add("Angelina");
waitingQueue.add("Johnny");
waitingQueue.add("Sachin");
System.out.println("WaitingQueue : " + waitingQueue);
// Check is a Queue is empty
System.out.println("is waitingQueue empty? : " + waitingQueue.isEmpty());
// Find the size of the Queue
System.out.println("Size of waitingQueue : " + waitingQueue.size());
// Check if the Queue contains an element
String name = "Johnny";
if(waitingQueue.contains(name)) {
System.out.println("WaitingQueue contains " + name);
} else {
System.out.println("Waiting Queue doesn't contain " + name);
}
// Get the element at the front of the Queue without removing it using element()
// The element() method throws NoSuchElementException if the Queue is empty
String firstPersonInTheWaitingQueue = waitingQueue.element();
System.out.println("First Person in the Waiting Queue (element()) : " + firstPersonInTheWaitingQueue);
// Get the element at the front of the Queue without removing it using peek()
// The peek() method is similar to element() except that it returns null if the Queue is empty
firstPersonInTheWaitingQueue = waitingQueue.peek();
System.out.println("First Person in the Waiting Queue : " + firstPersonInTheWaitingQueue);
}
}