代码优化 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);
时间: 2023-08-07 12:03:22 浏览: 54
有一些地方可以进行优化:
1. 在 `if (CollectionUtils.isEmpty(stringStringMap))` 中,你返回了一个 `"无数据"` 的字符串。但是这个字符串并没有什么实际用处,因为在下面的代码中并没有使用到它。因此,你可以直接在这个 if 语句中返回一个空的 List,这样可以避免后面的代码无意义地执行。
2. 在 `stringList.forEach(ar->{ EquDeviceTslProperty equDeviceTslProperty = JSON.parseObject(ar, EquDeviceTslProperty.class); excelResult.add(equDeviceTslProperty); });` 中,你使用了 forEach 循环遍历 `stringList`,并将遍历到的每个元素转换成一个 `EquDeviceTslProperty` 对象,然后将这个对象添加到 `excelResult` 中。但是,这个转换的过程可以使用 stream 和 map 操作来简化代码,如下所示:
```java
List<EquDeviceTslProperty> excelResult = stringList.stream()
.map(ar -> JSON.parseObject(ar, EquDeviceTslProperty.class))
.collect(Collectors.toList());
```
3. 在 `List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(deviceTslProperty);` 中,你查询了数据库,获取了一些设备的属性信息。但是,这个查询操作可能会比较耗时,因此你可以考虑将这个查询操作移到后面的代码中,只查询需要用到的属性信息。
4. 在 `Map<String, List<EquDeviceTslProperty>> listMap = equDeviceTslProperties.stream().collect(Collectors.groupingBy(EquDeviceTslProperty::getPropertyId));` 中,你使用了 stream 和 groupingBy 操作将 `equDeviceTslProperties` 中的元素按照 `propertyId` 分组。这个操作可以简化代码,但是可能会比较耗时,因此你可以考虑将 `equDeviceTslProperties` 中的元素排序,然后使用 for 循环遍历,将元素按照 `propertyId` 分组,这样可以提高代码的执行效率。
5. 在 `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)); });` 中,你使用了 forEach 循环遍历 `excelResult`,并根据每个元素的 `propertyId` 查找 `equDeviceTslProperties` 中对应的元素,然后更新它们的属性信息。但是,这个操作可能会比较耗时,因此你可以考虑将 `excelResult` 中的元素排序,然后使用 for 循环遍历,根据每个元素的 `propertyId` 查找对应的元素,并更新它们的属性信息,这样可以提高代码的执行效率。同时,你可以将不需要更新的元素添加到 `saveList` 中,然后一次性将它们插入到数据库中,这样可以减少数据库操作的次数。
综上所述,优化后的代码如下所示:
```java
List<EquDeviceTslProperty> excelResult = ExcelUtil.excelToShopIdList1(inputStream).stream()
.map(ar -> JSON.parseObject(ar, EquDeviceTslProperty.class))
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(excelResult)) {
return Collections.emptyList();
}
excelResult.sort(Comparator.comparing(EquDeviceTslProperty::getPropertyId));
List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(new EquDeviceTslProperty().setDeviceCode(excelResult.get(0).getDeviceCode()));
List<EquDeviceTslProperty> saveList = new ArrayList<>();
int i = 0;
for (EquDeviceTslProperty ar : excelResult) {
List<EquDeviceTslProperty> equDeviceTslProperties1 = new ArrayList<>();
while (i < equDeviceTslProperties.size() && equDeviceTslProperties.get(i).getPropertyId().equals(ar.getPropertyId())) {
equDeviceTslProperties1.add(equDeviceTslProperties.get(i));
i++;
}
if (equDeviceTslProperties1.isEmpty()) {
saveList.add(ar);
continue;
}
EquDeviceTslProperty equDeviceTslProperty = equDeviceTslProperties1.get(0);
equDeviceTslProperty.setValueType(ar.getValueType())
.setSortsIndex(ar.getSortsIndex())
.setPropertyName(ar.getPropertyName());
equDeviceTslPropertyService.updateEquDeviceTslProperty(equDeviceTslProperty);
}
equDeviceTslPropertyService.batchInsert(saveList);
```