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
- Check that the iterable has at least one element before calling any Means method
- Handle the empty case explicitly (return a default, skip, or log) rather than passing it to the mean
- 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
- All four mean methods (arithmetic, geometric, harmonic, quadratic) share this guard
- Check collection emptiness after filtering operations that may remove all elements
- Return Optional.empty() or a domain-specific default for empty data sets rather than letting the exception propagate
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
- Values array cannot be empty or null
- Number must be non-negative. Given:
- Number must be positive
- Input must be non-negative. Received:
- Base must be greater than 1.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/b987e0452fd9b6bd.
Report an issue: GitHub.