0
0
JavaHow-ToBeginner · 4 min read

How to Use Queue in Java: Syntax, Example, and Tips

In Java, a Queue is an interface that represents a collection designed for holding elements prior to processing. You can use classes like LinkedList or PriorityQueue to create a queue, then add elements with offer() and remove them with poll() or remove().
📐

Syntax

The Queue interface is part of the java.util package. You typically declare a queue like this:

  • Queue<Type> queue = new LinkedList<>(); - creates a queue using a linked list.
  • queue.offer(element); - adds an element to the queue.
  • queue.poll(); - removes and returns the head of the queue, or returns null if empty.
  • queue.peek(); - returns the head without removing it, or null if empty.
java
import java.util.Queue;
import java.util.LinkedList;

Queue<String> queue = new LinkedList<>();
queue.offer("first");
queue.offer("second");
String head = queue.poll();
String peek = queue.peek();
💻

Example

This example shows how to create a queue, add elements, and remove them in order (FIFO - first in, first out).

java
import java.util.Queue;
import java.util.LinkedList;

public class QueueExample {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();
        queue.offer("Apple");
        queue.offer("Banana");
        queue.offer("Cherry");

        System.out.println("Queue after adding elements: " + queue);

        String first = queue.poll();
        System.out.println("Removed element: " + first);

        String next = queue.peek();
        System.out.println("Next element to remove: " + next);

        System.out.println("Queue now: " + queue);
    }
}
Output
Queue after adding elements: [Apple, Banana, Cherry] Removed element: Apple Next element to remove: Banana Queue now: [Banana, Cherry]
⚠️

Common Pitfalls

Common mistakes when using queues in Java include:

  • Using remove() instead of poll() without checking if the queue is empty, which throws an exception.
  • Confusing offer() and add(). offer() returns false if it fails to add, while add() throws an exception.
  • Assuming peek() or poll() never return null. They return null if the queue is empty.
java
import java.util.Queue;
import java.util.LinkedList;

public class QueuePitfall {
    public static void main(String[] args) {
        Queue<String> queue = new LinkedList<>();

        // Wrong: remove() throws exception if empty
        // queue.remove(); // Uncommenting this line causes NoSuchElementException

        // Right: poll() returns null if empty
        String item = queue.poll();
        if (item == null) {
            System.out.println("Queue is empty, nothing to remove.");
        }
    }
}
Output
Queue is empty, nothing to remove.
📊

Quick Reference

MethodDescription
offer(E e)Adds element to the queue, returns false if fails
add(E e)Adds element, throws exception if fails
poll()Removes and returns head, or null if empty
remove()Removes and returns head, throws exception if empty
peek()Returns head without removing, or null if empty
element()Returns head without removing, throws exception if empty

Key Takeaways

Use Queue interface with classes like LinkedList to create queues in Java.
Add elements with offer() and remove with poll() to avoid exceptions on empty queues.
peek() lets you see the next element without removing it, returning null if empty.
Avoid remove() and element() unless you handle exceptions for empty queues.
Queues follow FIFO order: first element added is the first removed.