Java — Queue Interface | Code Factory

package com.example.java.programming.queue;import java.util.LinkedList;
import java.util.Queue;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String[] args) {
// Create and initialize a Queue using a LinkedList
Queue<String> queue = new LinkedList<>();
// Adding new elements to the Queue (The Enqueue operation)
queue.add("Narendra");
queue.add("Amit");
queue.add("Yogi");
queue.add("Nitin");
queue.add("Saurabh");
System.out.println("Queue : " + queue); // Removing an element from the Queue using remove() (The Dequeue operation)
// The remove() method throws NoSuchElementException if the Queue is empty
String name = queue.remove();
System.out.println("Removed from Queue : " + name + " | New Queue : " + queue);
// 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 = queue.poll();
System.out.println("Removed from Queue : " + name + " | New Queue : " + queue);
}
}
Queue : [Narendra, Amit, Yogi, Nitin, Saurabh]
Removed from Queue : Narendra | New Queue : [Amit, Yogi, Nitin, Saurabh]
Removed from Queue : Amit | New Queue : [Yogi, Nitin, Saurabh]
package com.example.java.programming.queue;import java.util.LinkedList;
import java.util.Queue;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String[] args) {
// Create and initialize a Queue using a LinkedList
Queue<String> queue = new LinkedList<>();
// Adding new elements to the Queue (The Enqueue operation)
queue.add("Narendra");
queue.add("Amit");
queue.add("Yogi");
queue.add("Nitin");
queue.add("Saurabh");
System.out.println("Queue : " + queue); // Check if a Queue is empty
System.out.println("is Queue empty? : " + queue.isEmpty());
// Find the size of the Queue
System.out.println("Size of Queue : " + queue.size());
// Check if the Queue contains an element
String name = "Johnny";
if (queue.contains(name)) {
System.out.println("Queue contains " + name);
} else {
System.out.println("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 firstPersonInTheQueue = queue.element();
System.out.println("First Person in the Queue (element()) : " + firstPersonInTheQueue);
// 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
firstPersonInTheQueue = queue.peek();
System.out.println("First Person in the Queue : " + firstPersonInTheQueue);
}
}
Queue : [Narendra, Amit, Yogi, Nitin, Saurabh]
is Queue empty? : false
Size of Queue : 5
Queue doesn't contain Johny
First Person in the Queue (element()) : Narendra
First Person in the Queue : Narendra
package com.example.java.programming.queue;import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author code.factory
*
*/
public class Test {
public static void main(String[] args) {
// Create and initialize a Queue using a LinkedList
Queue<String> queue = new LinkedList<>();
// Adding new elements to the Queue (The Enqueue operation)
queue.add("Narendra");
queue.add("Amit");
queue.add("Yogi");
queue.add("Nitin");
queue.add("Saurabh");
System.out.println("Iterating over a Queue using Java 8 forEach() :");
queue.forEach(name -> {
System.out.println(name);
});
System.out.println("\nIterating over a Queue using iterator() :");
Iterator<String> queueIterator = queue.iterator();
while (queueIterator.hasNext()) {
String name = queueIterator.next();
System.out.println(name);
}
System.out.println("\nIterating over a Queue using iterator() and Java 8 forEachRemaining() :");
queueIterator = queue.iterator();
queueIterator.forEachRemaining(name -> {
System.out.println(name);
});
System.out.println("\nIterating over a Queue using simple for-each loop :");
for (String name : queue) {
System.out.println(name);
}
}
}
Iterating over a Queue using Java 8 forEach() :
Narendra
Amit
Yogi
Nitin
Saurabh
Iterating over a Queue using iterator() :
Narendra
Amit
Yogi
Nitin
Saurabh
Iterating over a Queue using iterator() and Java 8 forEachRemaining() :
Narendra
Amit
Yogi
Nitin
Saurabh
Iterating over a Queue using simple for-each loop :
Narendra
Amit
Yogi
Nitin
Saurabh

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store