流式编程出现该异常
-
使用stream流时,未考虑数据不存在,直接使用get(),则会出现 No value present 异常。
-
异常代码:
ScreenPointInfo screenPoint = allPoints.stream()
.filter(screenPointInfo -> screenPointInfo.getPointCode().equals(devicePointCode)
&& screenPointInfo.getDataFrom().equals(new Integer(1)))
.findFirst().get();
- 修正后代码:
ScreenPointInfo screenPoint = allPoints.stream()
.filter(screenPointInfo -> screenPointInfo.getPointCode().equals(devicePointCode)
&& screenPointInfo.getDataFrom().equals(new Integer(1)))
.findFirst().orElse(null);