//按照型号进行分组
List<VResNeList> vResNeListList = vResNeLists.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(() ->
new TreeSet<>(Comparator.comparing(VResNeList::getTypeId))), ArrayList::new));
//分组后获取在线和离线
for (VResNeList vResNeList : vResNeListList) {
// 获取相同型号下的在线离线数量
List<VResNeList> down = vResNeLists.stream()
.filter(item -> (item.getTypeId().equals(vResNeList.getTypeId()) && (item.getState() == (0))))
.collect(Collectors.toList());
List<VResNeList> up = vResNeLists.stream()
.filter(item -> (item.getTypeId().equals(vResNeList.getTypeId()) && (item.getState() == (1))))
.collect(Collectors.toList());
System.out.println(down.size()+"离线" + up.size()+ "在线");
int total = (up.size()+ down.size());
if (total > 0){
// 创建一个数值格式化对象
NumberFormat numberFormat = NumberFormat.getInstance();
// 设置精确到小数点后2位
numberFormat.setMaximumFractionDigits(2);
String result = numberFormat.format((float)up.size()/ (float)total * 100);
//在线离线状态返回对象
NeReportStateVO neReportStateVO = new NeReportStateVO();
neReportStateVO.setName(vResNeList.getTypeName());
neReportStateVO.setOnlineTotal(up.size());
neReportStateVO.setOfflineTotal(down.size());
neReportStateVO.setOnlineRate(Double.valueOf(result));
neReportStateVO.setTotal(total);
neReportVOS.add(neReportStateVO);
}else {
//在线离线状态返回对象
NeReportStateVO neReportStateVO = new NeReportStateVO();
neReportVOS.add(neReportStateVO);
}
}
按照型号分组,分组后统计数量
最新推荐文章于 2023-08-25 10:46:36 发布