Creating a Queue and Performing basic operations like Enqueue and Dequeue

In this example, we will create a Queue and perform basic operations like Enqueue and Dequeue.

A Queue in Java is just an interface. We need a concrete implementation of the Queue interface to work with, in our programs. Let's use LinkedList class which implements the Queue interface and therefore it can be used as a Queue.

Creating a Queue and Performing basic operations like Enqueue and Dequeue

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);
    }
}
# Output
WaitingQueue : [Rajeev, Chris, John, Mark, Steven]
Removed from WaitingQueue : Rajeev | New WaitingQueue : [Chris, John, Mark, Steven]
Removed from WaitingQueue : Chris | New WaitingQueue : [John, Mark, Steven]

Comments