Key points about the Queue interface
- FIFO — a
Queuefollows First-In-First-Out: elements are added at the rear and removed from the front. - Insertion and removal operations —
Queuesupports insert, remove, and examine. For insertion:add(E e)oroffer(E e). For removal:remove()andpoll(). To examine or retrieve without removing:element()andpeek(). - Exception-throwing vs. non-exception-throwing methods — each operation has two variants:
one throws an exception on failure, the other returns a special value (
nullorfalse). For example,add(E e)throwsIllegalStateExceptionif no space is available, whileoffer(E e)simply returnsfalsein the same scenario.
Methods the Queue interface provides:

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