{"record":{"id":"b987e0452fd9b6bd","repo":"TheAlgorithms/Java","slug":"empty-list-given-for-mean-computation","errorCode":null,"errorMessage":"Empty list given for Mean computation.","messagePattern":"Empty list given for Mean computation\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/Means.java","lineNumber":140,"sourceCode":"     * @see <a href=\"https://en.wikipedia.org/wiki/Root_mean_square\">Quadratic\n     *      Mean</a>\n     */\n    public static Double quadratic(final Iterable<Double> numbers) {\n        checkIfNotEmpty(numbers);\n        double sumOfSquares = StreamSupport.stream(numbers.spliterator(), false).reduce(0d, (x, y) -> x + y * y);\n        int size = IterableUtils.size(numbers);\n        return Math.pow(sumOfSquares / size, 0.5);\n    }\n\n    /**\n     * Validates that the input iterable is not empty.\n     *\n     * @param numbers the input numbers to validate\n     * @throws IllegalArgumentException if the input is empty\n     */\n    private static void checkIfNotEmpty(final Iterable<Double> numbers) {\n        if (!numbers.iterator().hasNext()) {\n            throw new IllegalArgumentException(\"Empty list given for Mean computation.\");\n        }\n    }\n}\n","sourceCodeStart":122,"sourceCodeEnd":144,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/Means.java#L122-L144","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nDouble mean = Means.arithmetic(filteredNumbers);\n\n// after\nif (filteredNumbers == null || !filteredNumbers.iterator().hasNext()) {\n    return Optional.empty();  // or throw a domain-specific exception\n}\nDouble mean = Means.arithmetic(filteredNumbers);","handlingStrategy":"validation","validationCode":"if (numbers == null || !numbers.iterator().hasNext()) {\n    return Optional.<Double>empty();\n}\nDouble mean = Means.arithmetic(numbers);","typeGuard":"static boolean hasElements(Iterable<?> iterable) {\n    return iterable != null && iterable.iterator().hasNext();\n}","tryCatchPattern":"try {\n    Double mean = Means.arithmetic(numbers);\n} catch (IllegalArgumentException e) {\n    // input was empty — return default or skip\n    return Optional.empty();\n}","preventionTips":["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"],"tags":["math","validation","illegal-argument","statistics","empty-collection"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}