Calculate "average", "median" and "percentile" from a long array
Validate your inputs

The median and average methods will throw an exception if the input array is empty. Think of the preconditions and if there are safe assumptions (input will never be empty) then document it, possibly with an assert statement, for example:

assert numbers.length &gt; 0;


Note that the primary purpose of asset statements is documentation. Although a failed assert will crash the program, but only during development, as assert statements are removed from release builds, and so don't get executed in production. If you want to validate input, use exceptions instead.

Watch out for corner cases

This statement may overflow if the two numbers are too big:


return (numbers[middle - 1] + numbers[middle]) / 2.0;



To avoid overflow, you could divide each by 2 and then sum.

Simplify

If you omit the second and third parameters of Arrays.sort, by default it will use 0 and the length of the array. So you can omit these parameters in all your examples.

There is already a library method to calculate average:

return LongStream.of(numbers).average();