TheAlgorithms/Java · error · IllegalArgumentException

Empty list given for Mean computation.

Error message

Empty list given for Mean computation.

What it means

Thrown by the private checkIfNotEmpty(Iterable<Double>) method, invoked by arithmetic(), geometric(), harmonic(), and quadratic(). All four mean computations require at least one element to divide by; an empty iterable would cause a division by zero or undefined average. The check uses numbers.iterator().hasNext() to detect emptiness.

Source

Thrown at src/main/java/com/thealgorithms/maths/Means.java:140

     * @see <a href="https://en.wikipedia.org/wiki/Root_mean_square">Quadratic
     *      Mean</a>
     */
    public static Double quadratic(final Iterable<Double> numbers) {
        checkIfNotEmpty(numbers);
        double sumOfSquares = StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x + y * y);
        int size = IterableUtils.size(numbers);
        return Math.pow(sumOfSquares / size, 0.5);
    }

    /**
     * Validates that the input iterable is not empty.
     *
     * @param numbers the input numbers to validate
     * @throws IllegalArgumentException if the input is empty
     */
    private static void checkIfNotEmpty(final Iterable<Double> numbers) {
        if (!numbers.iterator().hasNext()) {
            throw new IllegalArgumentException("Empty list given for Mean computation.");
        }
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check that the iterable has at least one element before calling any Means method
  2. Handle the empty case explicitly (return a default, skip, or log) rather than passing it to the mean
  3. Use collection.isEmpty() or !iterator().hasNext() as a pre-check

Example fix

// before
Double mean = Means.arithmetic(filteredNumbers);

// after
if (filteredNumbers == null || !filteredNumbers.iterator().hasNext()) {
    return Optional.empty();  // or throw a domain-specific exception
}
Double mean = Means.arithmetic(filteredNumbers);
Defensive patterns

Strategy: validation

Validate before calling

if (numbers == null || !numbers.iterator().hasNext()) {
    return Optional.<Double>empty();
}
Double mean = Means.arithmetic(numbers);

Type guard

static boolean hasElements(Iterable<?> iterable) {
    return iterable != null && iterable.iterator().hasNext();
}

Try / catch

try {
    Double mean = Means.arithmetic(numbers);
} catch (IllegalArgumentException e) {
    // input was empty — return default or skip
    return Optional.empty();
}

Prevention

When it happens

Trigger: Calling Means.arithmetic(emptyList), Means.geometric(Collections.emptyList()), or any mean method with an empty Collection, Set, or custom Iterable. Note: passing null would cause a NullPointerException before this check, not this error.

Common situations: Filtering a collection to compute a mean, where the filter removes all elements. Reading from an empty database query result or empty stream-into-list. Edge case in data pipelines where some partitions have no data.

Related errors


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