Skip to content

Commit 1edf319

Browse files
authored
feat(maths): enhance Average with stream method and improved JavaDoc (TheAlgorithms#7369)
* docs(maths): improve JavaDoc for Average utility class * feat(maths): add stream-based averageStream method and validations * style: fix formatting * style: final formatting fix for CI checks * style: apply official clang-format to Average.java
1 parent e0a7223 commit 1edf319

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/main/java/com/thealgorithms/maths/Average.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package com.thealgorithms.maths;
22

3+
import java.util.Arrays;
4+
import java.util.OptionalDouble;
5+
36
/**
47
* A utility class for computing the average of numeric arrays.
5-
* This class provides static methods to calculate the average of arrays
6-
* of both {@code double} and {@code int} values.
8+
*
9+
* <p>This class provides static methods to calculate the arithmetic mean
10+
* of arrays of both {@code double} and {@code int} values. It also offers
11+
* a Stream-based alternative for modern, declarative usage.
12+
*
13+
* <p>All methods guard against {@code null} or empty inputs.
714
*/
815
public final class Average {
916

@@ -13,11 +20,14 @@ private Average() {
1320
}
1421

1522
/**
16-
* Computes the average of a {@code double} array.
23+
* Computes the arithmetic mean of a {@code double} array.
24+
*
25+
* <p>The average is calculated as the sum of all elements divided
26+
* by the number of elements: {@code avg = Σ(numbers[i]) / n}.
1727
*
18-
* @param numbers an array of {@code double} values
19-
* @return the average of the given numbers
20-
* @throws IllegalArgumentException if the input array is {@code null} or empty
28+
* @param numbers a non-null, non-empty array of {@code double} values
29+
* @return the arithmetic mean of the given numbers
30+
* @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
2131
*/
2232
public static double average(double[] numbers) {
2333
if (numbers == null || numbers.length == 0) {
@@ -31,11 +41,14 @@ public static double average(double[] numbers) {
3141
}
3242

3343
/**
34-
* Computes the average of an {@code int} array.
44+
* Computes the arithmetic mean of an {@code int} array.
45+
*
46+
* <p>The sum is accumulated in a {@code long} to prevent integer overflow
47+
* when processing large arrays or large values.
3548
*
36-
* @param numbers an array of {@code int} values
37-
* @return the average of the given numbers
38-
* @throws IllegalArgumentException if the input array is {@code null} or empty
49+
* @param numbers a non-null, non-empty array of {@code int} values
50+
* @return the arithmetic mean as a {@code long} (truncated toward zero)
51+
* @throws IllegalArgumentException if {@code numbers} is {@code null} or empty
3952
*/
4053
public static long average(int[] numbers) {
4154
if (numbers == null || numbers.length == 0) {
@@ -47,4 +60,21 @@ public static long average(int[] numbers) {
4760
}
4861
return sum / numbers.length;
4962
}
63+
64+
/**
65+
* Computes the arithmetic mean of a {@code double} array using Java Streams.
66+
*
67+
* <p>This method is a declarative alternative to {@link #average(double[])}.
68+
* Instead of throwing on empty input, it returns an empty {@link OptionalDouble},
69+
* following the convention of the Stream API.
70+
*
71+
* @param numbers an array of {@code double} values, may be {@code null} or empty
72+
* @return an {@link OptionalDouble} with the mean, or empty if input is null/empty
73+
*/
74+
public static OptionalDouble averageStream(double[] numbers) {
75+
if (numbers == null || numbers.length == 0) {
76+
return OptionalDouble.empty();
77+
}
78+
return Arrays.stream(numbers).average();
79+
}
5080
}

0 commit comments

Comments
 (0)