if (!ObjectUtils.isEmpty(gdPolylineDOS)) { return gdPolylineDOS.stream().findFirst().get().getPolyline(); } 这段代码有什么问题
时间: 2024-04-11 19:26:43 浏览: 93
这段代码也可能会引发空指针异常。虽然你通过 `ObjectUtils.isEmpty(gdPolylineDOS)` 来判断 `gdPolylineDOS` 是否为空,但是在 `gdPolylineDOS.stream().findFirst().get()` 这一行代码中,仍然有可能出现空指针异常。
如果 `gdPolylineDOS` 是一个空的集合,那么调用 `stream()` 方法将返回一个空的流对象。接着调用 `findFirst()` 方法,它将返回一个空的 Optional 对象。然后调用 `get()` 方法来获取 Optional 对象中的值时,由于值为空,将会抛出空指针异常。
为了避免这个问题,你可以使用 `ifPresent()` 方法来判断 Optional 对象是否包含值,并在存在值的情况下执行相应的操作。例如:
```java
if (!ObjectUtils.isEmpty(gdPolylineDOS)) {
Optional<GdPolylineDO> firstPolyline = gdPolylineDOS.stream().findFirst();
firstPolyline.ifPresent(polylineDO -> {
Polyline polyline = polylineDO.getPolyline();
// 进一步处理 polyline 对象
});
}
```
这样可以确保当集合不为空且存在元素时,才会执行相应的操作,避免空指针异常。
相关问题
//获取用户权限 List<DataPermissionSchemaEo> dataPermissionsRedisByUserUid = DataPermissionUtils.getDataPermissionsRedisByUserUid(UserAgent.get().getUserId()); if (CollectionUtils.isEmpty(dataPermissionsRedisByUserUid)) { return joinPoint.proceed(); } Map<String, DataPermissionSchemaEo> dataPermissionSchemaEoMap = dataPermissionsRedisByUserUid.stream().collect(Collectors.toMap(key -> key.getDatabaseCode() + key.getSchemaCode(), value -> value)); //获取用户对于这个表的行权限 DataPermissionSchemaEo dataPermissionSchemaEo = dataPermissionSchemaEoMap.get(databaseName + tableName); if(dataPermissionSchemaEo == null){ return joinPoint.proceed(); } List<SchemaRowRuleEo> schemaRowRuleList = dataPermissionSchemaEo.getSchemaRowRuleList(); if(CollectionUtils.isEmpty(schemaRowRuleList)){ return joinPoint.proceed(); }帮我优化这段代码
可以将代码优化为:
```java
List<DataPermissionSchemaEo> dataPermissionsRedisByUserUid = DataPermissionUtils.getDataPermissionsRedisByUserUid(UserAgent.get().getUserId());
if (CollectionUtils.isEmpty(dataPermissionsRedisByUserUid)) {
return joinPoint.proceed();
}
DataPermissionSchemaEo dataPermissionSchemaEo = dataPermissionsRedisByUserUid.stream()
.filter(d -> (d.getDatabaseCode() + d.getSchemaCode()).equals(databaseName + tableName))
.findFirst()
.orElse(null);
if (dataPermissionSchemaEo == null) {
return joinPoint.proceed();
}
List<SchemaRowRuleEo> schemaRowRuleList = dataPermissionSchemaEo.getSchemaRowRuleList();
if (CollectionUtils.isEmpty(schemaRowRuleList)) {
return joinPoint.proceed();
}
```
这里采用了:
1. 使用 Stream API 的 filter() 和 findFirst() 方法来优雅地查找用户对于这个表的行权限。
2. 使用 orElse(null) 方法来取代 if 判断,使得代码更加简洁。
3. 将变量声明和初始化放在一起,提高代码的可读性。
代码优化 List<String> stringList = ExcelUtil.excelToShopIdList1(inputStream); if (CollectionUtils.isEmpty(stringStringMap)) { return warn("无数据"); } List<EquDeviceTslProperty> excelResult = new ArrayList<>(); stringList.forEach(ar->{ EquDeviceTslProperty equDeviceTslProperty = JSON.parseObject(ar, EquDeviceTslProperty.class); excelResult.add(equDeviceTslProperty); }); EquDeviceTslProperty deviceTslProperty = new EquDeviceTslProperty(); deviceTslProperty.setDeviceCode(excelResult.get(0).getDeviceCode()); List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(deviceTslProperty); List<EquDeviceTslProperty> saveList = new ArrayList<>(); Map<String, List<EquDeviceTslProperty>> listMap = equDeviceTslProperties.stream().collect(Collectors.groupingBy(EquDeviceTslProperty::getPropertyId)); excelResult.forEach(ar->{ List<EquDeviceTslProperty> equDeviceTslProperties1 = listMap.get(ar.getPropertyId()); if (CollectionUtils.isEmpty(equDeviceTslProperties1)) { saveList.add(ar); return; } equDeviceTslProperties1.get(0).setValueType(ar.getValueType()) .setSortsIndex(ar.getSortsIndex()) .setPropertyName(ar.getPropertyName()); equDeviceTslPropertyService.updateEquDeviceTslProperty(equDeviceTslProperties1.get(0)); }); equDeviceTslPropertyService.batchInsert(saveList);
可以考虑使用Java8的stream API来简化代码:
```
List<String> stringList = ExcelUtil.excelToShopIdList1(inputStream);
if (CollectionUtils.isEmpty(stringList)) {
return warn("无数据");
}
List<EquDeviceTslProperty> excelResult = stringList.stream()
.map(ar -> JSON.parseObject(ar, EquDeviceTslProperty.class))
.collect(Collectors.toList());
String deviceCode = excelResult.get(0).getDeviceCode();
List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(new EquDeviceTslProperty().setDeviceCode(deviceCode));
Map<String, List<EquDeviceTslProperty>> listMap = equDeviceTslProperties.stream().collect(Collectors.groupingBy(EquDeviceTslProperty::getPropertyId));
List<EquDeviceTslProperty> saveList = excelResult.stream()
.filter(ar -> CollectionUtils.isEmpty(listMap.get(ar.getPropertyId())))
.collect(Collectors.toList());
listMap.forEach((propertyId, properties) -> {
excelResult.stream()
.filter(ar -> propertyId.equals(ar.getPropertyId()))
.findFirst()
.ifPresent(ar -> {
EquDeviceTslProperty property = properties.get(0);
property.setValueType(ar.getValueType())
.setSortsIndex(ar.getSortsIndex())
.setPropertyName(ar.getPropertyName());
equDeviceTslPropertyService.updateEquDeviceTslProperty(property);
});
});
equDeviceTslPropertyService.batchInsert(saveList);
```
这样可以更清晰地表达出代码的逻辑,并且减少了一些不必要的中间变量的使用。
阅读全文