数组 //需要筛选的数组 List homeVehicles = new ArrayList(); HomeVehicle homeVehicle = null; HomeDevice homeDevice; List infoList = vehicleInfoMapper.getHomeVehicel(group1.getId(),null); Long vehiId = null; Date signTime = null; Long
//需要筛选的数组 ListhomeVehicles = new ArrayList<>(); HomeVehicle homeVehicle = null; HomeDevice homeDevice; List infoList = vehicleInfoMapper.getHomeVehicel(group1.getId(),null); Long vehiId = null; Date signTime = null; Long deviId = null; for (HomeVehicle vehicle : infoList) { //以ID为分组 if(!Objects.equals(vehiId,vehicle.getVehiId())){ if(Objects.nonNull(homeVehicle)){ homeVehicles.add(homeVehicle); } vehiId = vehicle.getVehiId(); homeVehicle = vehicle; homeVehicle.setDevices(new ArrayList<>()); signTime = null; deviId = null; } //对时间排序 if(signTime == null){ signTime = vehicle.getSignalTime(); deviId = vehicle.getDeviId(); }else { if(vehicle.getSignalTime() != null && signTime.getTime() < vehicle.getSignalTime().getTime()){ signTime = vehicle.getSignalTime(); deviId = vehicle.getDeviId(); } } homeVehicle.setSignalTime(signTime); homeVehicle.setDeviId(deviId); if(Objects.isNull(vehicle.getDeviId())){ continue; } homeDevice = new HomeDevice(); homeDevice.setDeviName(vehicle.getDeviName()); homeDevice.setDeviId(vehicle.getDeviId()); homeDevice.setDeviOnline(vehicle.getDeviOnline()); homeDevice.setOnofTime(vehicle.getOnofTime()); homeVehicle.getDevices().add(homeDevice); } if(Objects.nonNull(homeVehicle)){ homeVehicles.add(homeVehicle); } groupVehicle.setVehicleInfos(homeVehicles); groupVehicleList.add(groupVehicle); }