Java provides numeric streams of int, long and double. If you have an array of one of those, you can find the min or max using Arrays.stream().
A comprehensive solution to the problem of reductions over primitive arrays is rather than implement min(), max() etc. for each array type, to instead implement the missing overrides of Arrays.stream(). Then you can use numeric stream methods such as .min(), .max(), .reduce(), and .generateStatistics(). Using a numeric stream avoids the overhead of sorting the array, converting all of the elements to wrapper types, or converting the whole array to int[] or double[].
public static IntStream stream(final byte[] bytes) {
return IntStream.range(0, bytes.length).map(i -> bytes[i]);
}
public static IntStream stream(final char[] chars) {
return IntStream.range(0, chars.length).map(i -> chars[i]);
}
public static IntStream stream(final short[] shorts) {
return IntStream.range(0, shorts.length).map(i -> shorts[i]);
}
public static DoubleStream stream(final float[] floats) {
return IntStream.range(0, floats.length).mapToDouble(i -> floats[i]);
}
The overhead of converting to int should be minimal since the JVM represents smaller integral values as int anyway, except in arrays.
The overhead of converting from float to double is very slightly larger, so if you really care about performance you still might want to iterate over the array. Since there are float overrides for Math::max, Math::min , and so on, you could use this reduce method with them:
@FunctionalInterface
public interface FloatBinaryOperator {
float applyAsFloat(float left, float right);
}
public static float reduce(final float[] floats, final float identity, final FloatBinaryOperator operator) {
float result = identity;
for (float element : floats) {
result = operator.applyAsFloat(result, element);
}
return result;
}
float[] floats = getFloats();
float min = reduce(floats, Float.POSITIVE_INFINITY, Math::min);
float max = reduce(floats, Float.NEGATIVE_INFINITY, Math::max);
I haven't benchmarked converting to DoubleStream vs using this reduce() method. As always, do your own profiling before making performance decisions.
Collections.max(Arrays.asList()).