【发布时间】:2021-05-10 07:55:57
【问题描述】:
我已经看到了很多解决这个问题的方法,但无论我尝试什么,IDEA 仍然报错。
考虑以下块:
double testDouble= customClass.stream()
.mapToDouble(CustomClass::getDouble)
.max()
.getAsDouble();
这会报告'OptionalDouble.getAsDouble()' without 'isPresent()' check 的警告。
如果我尝试这个,那么它不会编译:
double testDouble= customClass.stream()
.mapToDouble(CustomClass::getDouble)
.max().orElseThrow(IllegalStateException::new)
.getAsDouble();
这个也不行:
double testDouble= customClass.stream()
.mapToDouble(CustomClass::getDouble)
.max().orElse(null)
.getAsDouble();
或者这个:
double testDouble= customClass.stream()
.mapToDouble(CustomClass::getDouble)
.max().isPresent()
.getAsDouble();
虽然我知道这些可选的双精度值永远不会为空,但我想解决它们,以免出现警告。
谁能指出我哪里出错了?
【问题讨论】:
标签: java java-stream double optional