Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create java-queue-example #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions java-queue-example
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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);

}
}