CKTIS / Java
Long bounds and the Long.max and Long.min methods
Distinguish the bounds of `long` from methods that compare two values, then choose the right identity for a maximum or minimum reduction.
Version: Java SE 21
Syntax
java.lang.Long (Java SE 21)
public static final long MIN_VALUE // -9223372036854775808
public static final long MAX_VALUE // 9223372036854775807
public static long max(long a, long b)
public static long min(long a, long b)
LongStream.of(values).reduce(Long.MIN_VALUE, Long::max)
LongStream.of(values).reduce(Long.MAX_VALUE, Long::min)
// values is a long[]; Long::max and Long::min are LongBinaryOperator method references here.Long.MIN_VALUE/MAX_VALUE refers to two public static final long fields on java.lang.Long, not methods. Long.MIN_VALUE and Long.MAX_VALUE are the inclusive bounds of the signed 64-bit long type: -9223372036854775808 and 9223372036854775807, respectively. They do not depend on the input data.
Long.max/min refers to two static methods: Long.max(a, b) returns the greater of two long values in signed order, and Long.min(a, b) returns the smaller. If the inputs are equal, both methods return that value. The methods have been available since Java 8 and behave like the corresponding (long, long) overloads in Math.
In LongStream.reduce, the references Long::max and Long::min fit LongBinaryOperator: they take two primitive long values and return a long without boxing. Use Long.MIN_VALUE as the identity for maximum because Long.max(Long.MIN_VALUE, x) == x for every long. Use Long.MAX_VALUE for minimum because Long.min(Long.MAX_VALUE, x) == x. Zero is not an identity for either operation: it would give the wrong maximum for all-negative data or the wrong minimum for all-positive data.
A reduction with an identity returns that identity on an empty stream. The same value can be a real result on nonempty input, so it cannot tell you whether the stream was empty. If that distinction matters, use LongStream.max() or LongStream.min(): they return an empty OptionalLong when the stream has no elements.
Examples
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) {
long[] values = {-9L, -2L, -5L};
long max = LongStream.of(values).reduce(Long.MIN_VALUE, Long::max);
long min = LongStream.of(values).reduce(Long.MAX_VALUE, Long::min);
System.out.println("max = " + max);
System.out.println("min = " + min);
System.out.println("empty max with identity = "
+ LongStream.empty().reduce(Long.MIN_VALUE, Long::max));
System.out.println("empty min with identity = "
+ LongStream.empty().reduce(Long.MAX_VALUE, Long::min));
System.out.println("empty max is absent = " + LongStream.empty().max().isEmpty());
}
}
Result
max = -2 min = -9 empty max with identity = -9223372036854775808 empty min with identity = 9223372036854775807 empty max is absent = true
The first two lines show the extrema of the negative inputs. The next two show what an identity-based reduction returns on empty input, and the last shows that `max()` reports the absence of a result.
Related lessons
- Java · Lesson 31 · Advanced · Version: 2