代码优化 EquDeviceTslProperty tslProperty = new EquDeviceTslProperty(); tslProperty.setDeviceCode(deviceCode); List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(tslProperty); if (equDeviceTslProperties.size() > 8) { equDeviceTslProperties = equDeviceTslProperties.subList(0, 8); } DeviceDataShows = equDeviceTslProperties.stream() .map(ar -> { RealTimeData deviceRealTimeData = this.getDeviceRealTimeData(deviceCode, ar.getPropertyId()); DeviceDataShow deviceDataShow = new DeviceDataShow(); BeanUtils.copyBeanProp(deviceDataShow, deviceRealTimeData); BeanUtils.copyBeanProp(deviceDataShow, ar); return deviceDataShow; }) .collect(Collectors.toList());
时间: 2024-02-14 09:05:35 浏览: 64
这段代码已经进行了比较好的优化,但是还有以下几点可以改进:
1. 可以对 `getDeviceRealTimeData()` 方法进行优化,避免多次调用该方法。
2. 可以考虑对 `BeanUtils.copyBeanProp()` 方法进行封装,避免重复的代码。
3. 可以把 `EquDeviceTslProperty` 类和 `RealTimeData` 类的属性名进行对比,如果属性名相同可以直接进行赋值而不需要使用 `BeanUtils.copyBeanProp()` 方法。
下面是进一步优化后的代码:
```java
if (StringUtils.isNotEmpty(deviceCode)) {
try {
EquDeviceTslProperty tslProperty = new EquDeviceTslProperty();
tslProperty.setDeviceCode(deviceCode);
List<EquDeviceTslProperty> equDeviceTslProperties = equDeviceTslPropertyService.selectEquDeviceTslPropertyList(tslProperty);
if (equDeviceTslProperties.size() > 8) {
equDeviceTslProperties = equDeviceTslProperties.subList(0, 8);
}
Map<String, RealTimeData> realTimeDataMap = new HashMap<>();
equDeviceTslProperties.forEach(ar -> {
RealTimeData deviceRealTimeData = realTimeDataMap.get(ar.getPropertyId());
if (deviceRealTimeData == null) {
deviceRealTimeData = this.getDeviceRealTimeData(deviceCode, ar.getPropertyId());
realTimeDataMap.put(ar.getPropertyId(), deviceRealTimeData);
}
DeviceDataShow deviceDataShow = new DeviceDataShow();
deviceDataShow.setDeviceCode(deviceCode);
deviceDataShow.setPropertyId(ar.getPropertyId());
deviceDataShow.setPropertyName(ar.getPropertyName());
deviceDataShow.setPropertyValue(deviceRealTimeData.getPropertyValue());
deviceDataShow.setUnit(ar.getUnit());
DeviceDataShows.add(deviceDataShow);
});
} catch (Exception e) {
log.error("获取设备数据失败!", e);
}
}
```
这里我将 `RealTimeData` 对象缓存到了一个 `Map` 中,以避免多次调用 `getDeviceRealTimeData()` 方法。同时,我对 `DeviceDataShow` 对象的属性进行了直接赋值,避免了使用 `BeanUtils.copyBeanProp()` 方法。
阅读全文
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![none](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<!DOCTYPE HTML> <html> <head> <title>搜索网站</title> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <noscript></noscript> </head> <body> 你好 尊敬的用户,欢迎使用本网站 本网站将为将您提供一下服务,如您有更好的意见,欢迎致信我们的邮箱 2653379060@qq.com 感谢您的支持 首页 热搜 联系 关于 <script src="assets/js/jquery.min.js"></script> <script src="assets/js/skel.min.js"></script> <script src="assets/js/util.js"></script> <script src="assets/js/main.js"></script> </body> </html>要在这段代码的左下角放一个轮播图,代码应该怎么写
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)