Java — Queue Interface | Code Factory

Code Factory
4 min readJun 22, 2020

--

Donate : Link

WordPress Blog : Link

A Queue is a First In First Out (FIFO) data structure. It models a queue in real-life. Yes, the one that you might have seen in front of a movie theater, a shopping mall, a metro, or a bus.

Just like queues in real life, new elements in a Queue data structure are added at the back and removed from the front. A Queue can be visualized as shown in the figure below.

The process of adding an element at the back of the Queue is called Enqueue, and the process of removing an element from the front of the Queue is called Dequeue.

Java provides a Queue interface which is part of Java’s collections framework. The figure below depicts the position of Queue interface in Collections hierarchy -

A Queue in Java is just an interface. We need a concrete implementation of the Queue interface to work with, in our programs.

As shown in the diagram above, the LinkedList class implements the Queue interface and therefore it can be used as a Queue.

Example 1 :

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);
}
}

Output :

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]

Example 2 :

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);
}
}

Output :

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

Example 3 :

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);
}
}
}

Output :

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

--

--