Skip to content
thesarfo

Reference

Queue

FIFO behavior, insertion/removal methods, and a LinkedList-backed walkthrough of Java's Queue interface.

views 0

Key points about the Queue interface

  1. FIFO — a Queue follows First-In-First-Out: elements are added at the rear and removed from the front.
  2. Insertion and removal operationsQueue supports insert, remove, and examine. For insertion: add(E e) or offer(E e). For removal: remove() and poll(). To examine or retrieve without removing: element() and peek().
  3. Exception-throwing vs. non-exception-throwing methods — each operation has two variants: one throws an exception on failure, the other returns a special value (null or false). For example, add(E e) throws IllegalStateException if no space is available, while offer(E e) simply returns false in the same scenario.

Methods the Queue interface provides:

Queue interface methods

Queue interface with its LinkedList implementation

public class QueueExample {
public static void main(String[] args) {
// Create and initialize a Queue using a LinkedList
Queue<String> elementQueue = new LinkedList<>();
// Adding new elements to the Queue (the enqueue operation)
elementQueue.add("element1");
elementQueue.add("element2");
elementQueue.add("element3");
elementQueue.add("element4");
System.out.println("WaitingQueue : " + elementQueue);
// Removing an element from the Queue using remove() (the dequeue operation)
// remove() throws NoSuchElementException if the Queue is empty
String name = elementQueue.remove();
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + elementQueue);
// Removing an element from the Queue using poll()
// poll() is like remove() except it returns null if the Queue is empty
name = elementQueue.poll();
System.out.println("Removed from WaitingQueue : " + name + " | New WaitingQueue : " + elementQueue);
}
}

More Queue methods

public class QueueSizeSearchFrontExample {
public static void main(String[] args) {
Queue<String> elementQueue = new LinkedList<>();
elementQueue.add("element1");
elementQueue.add("element2");
elementQueue.add("element3");
elementQueue.add("element4");
System.out.println("WaitingQueue : " + elementQueue);
// Check if a Queue is empty
System.out.println("is waitingQueue empty? : " + elementQueue.isEmpty());
// Find the size of the Queue
System.out.println("Size of waitingQueue : " + elementQueue.size());
// Check if the Queue contains an element
String name = "Johnny";
if (elementQueue.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()
// element() throws NoSuchElementException if the Queue is empty
String firstElementInTheWaitingQueue = elementQueue.element();
System.out.println("Waiting Queue (element()) : " + firstElementInTheWaitingQueue);
// Get the element at the front of the Queue without removing it, using peek()
// peek() is like element() except it returns null if the Queue is empty
firstElementInTheWaitingQueue = elementQueue.peek();
System.out.println("Waiting Queue : " + firstElementInTheWaitingQueue);
}
}