现有List<DeviceVO> list,DeviceVO有属性area(地名),up_status_1(状态,在线为“101”或“201”);根据area地名分组统计up_status_1在线数,离线数,并统计总数组?返回的结果为List<G4DeviceStateVO> rList,G4DeviceStateVO有属性totalNum, onlineNum, offlineNum,area_name。
思路:在map中写入key为DeviceVO的地名area,value为对应DeviceVO的List;再对map中的key对应的list进行统计,将结果写入G4DeviceStateVO中后拼成List。
List<DeviceVO>list=deviceMgrService.getDeviceDataInfo(deviceVO);
List<G4DeviceStateVO> rList = new ArrayList<G4DeviceStateVO>();
HashMap<String,List<DeviceVO>> map = new HashMap<String,List<DeviceVO>>();
for(DeviceVO deviceVO : list){
List<DeviceVO> tempList = map.get(deviceVO.getArea());
if(tempList == null){
tempList = new ArrayList();
tempList.add(deviceVO);
map.put(deviceVO.getArea(), tempList);
}
else{
tempList.add(deviceVO);
}
}
int Totalonline = 0;
int Totaloffline = 0;
for(String area : map.keySet()){
int online = 0;
int offline = 0;
for(DeviceVO deviceVO :map.get(area)){
if(("101").equals(deviceVO.getUp_status_1())||("201").equals(deviceVO.getUp_status_1())){
online++;
Totalonline++;
}else{
offline++;
Totaloffline++;
}
}
G4DeviceStateVO g4DeviceStateVO = new G4DeviceStateVO();
g4DeviceStateVO.setArea_name(area);
g4DeviceStateVO.setTotalNum(String.valueOf(online+offline));
g4DeviceStateVO.setOnlineNum(String.valueOf(online));
g4DeviceStateVO.setOfflineNum(String.valueOf(offline));
rList.add(g4DeviceStateVO);
}
G4DeviceStateVO g4 = new G4DeviceStateVO();
g4.setArea_name("总计");
g4.setTotalNum(String.valueOf(Totalonline+Totaloffline));
g4.setOnlineNum(String.valueOf(Totalonline));
g4.setOfflineNum(String.valueOf(Totaloffline));
rList.add(g4);