OptionalInt max = IntStream.of(arr).max();
Run Code Online (Sandbox Code Playgroud)
或者
int maximum = 0;
for (int i : arr) {
maximum = Math.max(i, maximum);
}
Run Code Online (Sandbox Code Playgroud)
这两种方法都返回最大值。有什么OptionalInt不同?根据 的定义OptionalInt,如果值存在,则返回,getAsInt()但如果值不存在,则抛出Exception。