Skip to content

Commit 45f13a3

Browse files
author
zihluwang
committed
fix: validate non-empty list before sorting
Move the empty list check before sorting to avoid processing an empty collection and provide clearer error handling.
1 parent bbc5215 commit 45f13a3

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

num4j/src/main/java/com/onixbyte/nums/PercentileCalculator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ private PercentileCalculator() {
7272
* @return a {@code Double} value representing the calculated percentile
7373
*/
7474
public static Double calculatePercentile(List<Double> values, Double percentile) {
75-
var sorted = values.stream().sorted().toList();
76-
if (sorted.isEmpty()) {
75+
if (values.isEmpty()) {
7776
throw new IllegalArgumentException("Unable to sort an empty list.");
7877
}
78+
var sorted = values.stream().sorted().toList();
7979

8080
var rank = percentile / 100. * (sorted.size() - 1);
8181
var lowerIndex = (int) Math.floor(rank);

0 commit comments

Comments
 (0)