List<String> monthByYear = DateUtils.getMonthByYear(year); StringBuilder esIndexBuilder = new StringBuilder(); for (String monthStr : monthByYear) { String tempEsIndex = StrUtil.join(StrUtil.UNDERLINE, TP_PORTFLUX, sysId1, year, monthStr, CommonEnum.STAT_MONTH.getCode()); esIndexBuilder.append(tempEsIndex).append(","); } esIndex = esIndexBuilder.toString(); 最后一位没有逗号
时间: 2023-11-22 08:53:08 浏览: 37
可以使用以下代码去掉最后一个逗号:
```
List<String> monthByYear = DateUtils.getMonthByYear(year);
StringBuilder esIndexBuilder = new StringBuilder();
for (int i = 0; i < monthByYear.size(); i++) {
String monthStr = monthByYear.get(i);
String tempEsIndex = StrUtil.join(StrUtil.UNDERLINE, TP_PORTFLUX, sysId1, year, monthStr, CommonEnum.STAT_MONTH.getCode());
esIndexBuilder.append(tempEsIndex);
if (i != monthByYear.size() - 1) {
esIndexBuilder.append(",");
}
}
esIndex = esIndexBuilder.toString();
```
这样就可以保证最后一位没有逗号了,同时也能保证其他位置都有逗号。
相关问题
List<String> monthByYear = DateUtils.getMonthByYear(year); for (int i = 0; i < monthByYear.size(); i++) { String tempEsIndex = StrUtil.join(StrUtil.UNDERLINE, TP_PORTFLUX, sysId1, year, monthByYear.get(i), CommonEnum.STAT_MONTH.getCode()); esIndex = tempEsIndex + ","; } 优化这段代码
可以优化成以下这样:
```
List<String> monthByYear = DateUtils.getMonthByYear(year);
StringBuilder esIndexBuilder = new StringBuilder();
for (String month : monthByYear) {
String tempEsIndex = StrUtil.join(StrUtil.UNDERLINE, TP_PORTFLUX, sysId1, year, month, CommonEnum.STAT_MONTH.getCode());
esIndexBuilder.append(tempEsIndex).append(",");
}
String esIndex = esIndexBuilder.toString();
```
这里使用了 StringBuilder 来拼接字符串,避免了每次迭代都创建一个新的字符串对象,提高了效率。另外,使用了 foreach 循环来遍历月份,代码更加简洁易读。
代码优化 List<String> allRows = this.getHBaseRows(); if (CollectionUtils.isEmpty(allRows)) { return success("暂无数据"); } // 数据转换 List<PositionInfo> allInfos = allRows.stream() .map(ar -> JSONObject.parseObject(ar, PositionInfo.class)) .filter(ar-> ar.getTimestamp() > DateUtils.addHours(new Date(),-1).getTime()) .collect(Collectors.toList()); // 获取每个用户最近一次的记录 List<PositionInfo> result = new ArrayList<>(allInfos.stream() .collect(Collectors.toMap(PositionInfo::getUserId, Function.identity(), BinaryOperator.maxBy(Comparator.comparing(PositionInfo::getTimestamp)))) .values());
这段代码可以进行以下优化:
1. 使用Optional避免空指针异常
可以使用Optional避免空指针异常,如下:
```
List<String> allRows = this.getHBaseRows();
return Optional.ofNullable(allRows)
.filter(CollectionUtils::isNotEmpty)
.map(rows -> {
// 数据转换
List<PositionInfo> allInfos = rows.stream()
.map(ar -> JSONObject.parseObject(ar, PositionInfo.class))
.filter(ar -> ar.getTimestamp() > DateUtils.addHours(new Date(), -1).getTime())
.collect(Collectors.toList());
// 获取每个用户最近一次的记录
List<PositionInfo> result = new ArrayList<>(allInfos.stream()
.collect(Collectors.toMap(PositionInfo::getUserId, Function.identity(),
BinaryOperator.maxBy(Comparator.comparing(PositionInfo::getTimestamp))))
.values());
return success(result);
})
.orElse(success("暂无数据"));
```
2. 使用Lambda表达式简化代码
可以使用Lambda表达式简化代码,如下:
```
List<String> allRows = this.getHBaseRows();
return Optional.ofNullable(allRows)
.filter(CollectionUtils::isNotEmpty)
.map(rows -> rows.stream()
.map(ar -> JSONObject.parseObject(ar, PositionInfo.class))
.filter(ar -> ar.getTimestamp() > DateUtils.addHours(new Date(), -1).getTime())
.collect(Collectors.groupingBy(PositionInfo::getUserId,
Collectors.collectingAndThen(Collectors.maxBy(Comparator.comparing(PositionInfo::getTimestamp)),
Optional::get)))
.values())
.map(ArrayList::new)
.map(this::success)
.orElse(success("暂无数据"));
```
这段代码使用了groupingBy方法将数据按照userId分组,然后使用collectingAndThen方法将每组数据取最大的timestamp,最后使用values方法获取所有分组的结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)