Skip to content

Commit b747189

Browse files
Create java-queue-example
1 parent 8429a7b commit b747189

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

java-queue-example

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import java.util.Iterator;
2+
import java.util.LinkedList;
3+
import java.util.Queue;
4+
5+
public class IterateOverQueueExample {
6+
public static void main(String[] args) {
7+
Queue<String> waitingQueue = new LinkedList<>();
8+
9+
waitingQueue.add("John");
10+
waitingQueue.add("Brad");
11+
waitingQueue.add("Angelina");
12+
waitingQueue.add("Julia");
13+
14+
System.out.println("=== Iterating over a Queue using Java 8 forEach() ===");
15+
waitingQueue.forEach(name -> {
16+
System.out.println(name);
17+
});
18+
19+
System.out.println("\n=== Iterating over a Queue using iterator() ===");
20+
Iterator<String> waitingQueueIterator = waitingQueue.iterator();
21+
while (waitingQueueIterator.hasNext()) {
22+
String name = waitingQueueIterator.next();
23+
System.out.println(name);
24+
}
25+
26+
System.out.println("\n=== Iterating over a Queue using iterator() and Java 8 forEachRemaining() ===");
27+
waitingQueueIterator = waitingQueue.iterator();
28+
waitingQueueIterator.forEachRemaining(name -> {
29+
System.out.println(name);
30+
});
31+
32+
System.out.println("\n=== Iterating over a Queue using simple for-each loop ===");
33+
for(String name: waitingQueue) {
34+
System.out.println(name);
35+
}
36+
}
37+
}
38+
39+
************************************************************************************************************************************************************************
40+
41+
import java.util.LinkedList;
42+
import java.util.Queue;
43+
44+
public class QueueExample {
45+
public static void main(String[] args) {
46+
// Create and initialize a Queue using a LinkedList
47+
Queue<String> waitingQueue = new LinkedList<>();
48+
49+
// Adding new elements to the Queue (The Enqueue operation)
50+
waitingQueue.add("Rajeev");
51+
waitingQueue.add("Chris");
52+
waitingQueue.add("John");
53+
waitingQueue.add("Mark");
54+
waitingQueue.add("Steven");
55+
56+
System.out.println("WaitingQueue : " + waitingQueue);
57+
58+
// Removing an element from the Queue using remove() (The Dequeue operation)
59+
// The remove() method throws NoSuchElementException if the Queue is empty
60+
String name = waitingQueue.remove();
61+
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
62+
63+
// Removing an element from the Queue using poll()
64+
// The poll() method is similar to remove() except that it returns null if the Queue is empty.
65+
name = waitingQueue.poll();
66+
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + waitingQueue);
67+
}
68+
}
69+
70+
***********************************************************************************************************************************************************************
71+
72+
import java.util.LinkedList;
73+
import java.util.Queue;
74+
75+
public class QueueSizeSearchFrontExample {
76+
public static void main(String[] args) {
77+
Queue<String> waitingQueue = new LinkedList<>();
78+
79+
waitingQueue.add("Jennifer");
80+
waitingQueue.add("Angelina");
81+
waitingQueue.add("Johnny");
82+
waitingQueue.add("Sachin");
83+
84+
System.out.println("WaitingQueue : " + waitingQueue);
85+
86+
// Check is a Queue is empty
87+
System.out.println("is waitingQueue empty? : " + waitingQueue.isEmpty());
88+
89+
// Find the size of the Queue
90+
System.out.println("Size of waitingQueue : " + waitingQueue.size());
91+
92+
// Check if the Queue contains an element
93+
String name = "Johnny";
94+
if(waitingQueue.contains(name)) {
95+
System.out.println("WaitingQueue contains " + name);
96+
} else {
97+
System.out.println("Waiting Queue doesn't contain " + name);
98+
}
99+
100+
// Get the element at the front of the Queue without removing it using element()
101+
// The element() method throws NoSuchElementException if the Queue is empty
102+
String firstPersonInTheWaitingQueue = waitingQueue.element();
103+
System.out.println("First Person in the Waiting Queue (element()) : " + firstPersonInTheWaitingQueue);
104+
105+
// Get the element at the front of the Queue without removing it using peek()
106+
// The peek() method is similar to element() except that it returns null if the Queue is empty
107+
firstPersonInTheWaitingQueue = waitingQueue.peek();
108+
System.out.println("First Person in the Waiting Queue : " + firstPersonInTheWaitingQueue);
109+
110+
}
111+
}
112+

0 commit comments

Comments
 (0)