TheAlgorithms/Java · error · NoSuchElementException

Queue is empty

Error message

Queue is empty

What it means

Thrown by QueueByTwoStacks.get() as a NoSuchElementException when both internal stacks are empty. get() first transfers any pending elements from enqueueStk to dequeueStk; only if dequeueStk is still empty afterward does it throw. This means the queue is genuinely empty (no put() since the last full drain).

Source

Thrown at src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java:56

    }

    /**
     * Removes and returns the element at the front of the queue.
     * If `dequeueStk` is empty, it transfers all elements from
     * `enqueueStk` to `dequeueStk` to maintain the correct FIFO
     * (First-In-First-Out) order before popping.
     *
     * @return The element at the front of the queue.
     * @throws NoSuchElementException If the queue is empty.
     */
    public T get() {
        if (dequeueStk.isEmpty()) {
            while (!enqueueStk.isEmpty()) {
                dequeueStk.push(enqueueStk.pop());
            }
        }
        if (dequeueStk.isEmpty()) {
            throw new NoSuchElementException("Queue is empty");
        }
        return dequeueStk.pop();
    }

    /**
     * Returns the total number of elements currently in the queue.
     * This is the sum of the sizes of both stacks.
     *
     * @return The number of elements in the queue.
     */
    public int size() {
        return enqueueStk.size() + dequeueStk.size();
    }

    /**
     * Returns a string representation of the queue, showing the elements
     * in the correct order (from front to back).
     * The `dequeueStk` is first cloned, and then all elements from the

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Track emptiness via size() (enqueueStk + dequeueStk sizes) and guard get() with size() > 0.
  2. Bound get() loops by the number of put() calls actually made.
  3. Catch NoSuchElementException when an empty queue is an expected outcome.

Example fix

// before
T v = queue.get(); // throws when empty

// after
T v = null;
if (queue.size() > 0) {
    v = queue.get();
}
Defensive patterns

Strategy: validation

Validate before calling

T v = null;
if (queue.size() > 0) {
    v = queue.get();
}

Type guard

boolean nonEmpty = queue.size() > 0;

Try / catch

null

Prevention

When it happens

Trigger: Calling get() on a freshly constructed QueueByTwoStacks, or calling get() more times than put(). The transfer step means a get() right after put() calls succeeds; the throw only happens when there is nothing on either stack.

Common situations: Consumer thread calling get() before the producer has put() anything; drain loop that calls get() one time too many; misuse where get() is the only emptiness signal; assuming get() blocks (it does not — this is non-blocking).

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/fad81098cdded73a. Report an issue: GitHub.