While others have already answered why Math.maxit is not a variable, they have not answered why such a method is not created when introducing variational functions.
I donβt even know this (there is an open error report ), so I can only guess:
It is true that it is not implemented in Math, but if we look at it Collections, there will be the following method:
public static <T extends Object & Comparable<? super T>> T max(
Collection<? extends T> coll) {
...
}
( ), Collections.max(Arrays.asList(-13, 12, 1337, 9)); , , .
: , , Comparable.
, , Collections -, . JDK8 :
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
int max(List<Integer> list) {
Optional<Integer> opt = list.stream().max((a,b) -> a-b);
return opt.orElse(Integer.MAX_VALUE);
}
max(Arrays.asList(-13, 12, 1337, 9));
max(Arrays.asList());
Project Lambda, . Lambdas . :
import static java.util.Comparators.naturalOrder;
Arrays.asList(-13, 12, 1337, 9)
.stream()
.max(naturalOrder())
.ifPresent(System.out::println);
max reduce:
Arrays.asList(-13, 12, 1337, 9)
.stream()
.reduce((a,b) -> a > b ? a : b)
.ifPresent(System.out::println); // 1337
- Optional. - , .
, Math.max:
- -
- . , (
List, Set, Stream, Iterator ..) - .
- .
.stream() .parallelStream()