怎么实现List中的对象分组统计?

       现有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);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值