输出解题思路及代码实现到一个类中并将代码按功能点进行拆分 每步代码上方要有注释。现已根据统计条件中的开始结束日期查出过车记录集合ListAnalysisJudgmentVehicleVO并已按照gcsj升序排序AnalysisJudgmentVehicleVO属性有sbidsbmchpzlhphmgcsj均为String类型。1根据过车记录将数据按照hpzl+hphm进行分组用lamda表达式处理
public class AnalysisJudgmentVehicle {
/**
* 根据过车记录,将数据按照hpzl+hphm进行分组
* @param list 过车记录集合
* @return 分组后的过车记录Map
*/
public Map<String, List<AnalysisJudgmentVehicleVO>> groupByHpzlHphm(List<AnalysisJudgmentVehicleVO> list) {
Map<String, List<AnalysisJudgmentVehicleVO>> map = list.stream()
.collect(Collectors.groupingBy(vo -> vo.getHpzl() + vo.getHphm()));
return map;
}
/**
* 比较两个过车记录之间的时间间隔是否小于x分钟
* @param vo1 过车记录1
* @param vo2 过车记录2
* @param x 时间间隔,单位为分钟
* @return 是否小于x分钟
*/
public boolean isWithinXMinutes(AnalysisJudgmentVehicleVO vo1, AnalysisJudgmentVehicleVO vo2, int x) {
LocalDateTime time1 = LocalDateTime.parse(vo1.getGcsj(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime time2 = LocalDateTime.parse(vo2.getGcsj(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
long seconds = Duration.between(time1, time2).getSeconds();
return seconds < x * 60;
}
/**
* 对分组后的过车记录进行计算,得到伴随车辆列表
* @param map 分组后的过车记录Map
* @param x 时间间隔,单位为分钟
* @param y 连续通过相同设备的次数
* @return 伴随车辆列表
*/
public List<AnalysisJudgmentVehicleWithFollowVO> calculateFollowVehicles(Map<String, List<AnalysisJudgmentVehicleVO>> map, int x, int y) {
List<AnalysisJudgmentVehicleWithFollowVO> followList = new ArrayList<>();
for (String key : map.keySet()) {
List<AnalysisJudgmentVehicleVO> groupList = map.get(key);
for (int i = 0; i < groupList.size(); i++) {
AnalysisJudgmentVehicleVO vo1 = groupList.get(i);
for (int j = i + 1; j < groupList.size(); j++) {
AnalysisJudgmentVehicleVO vo2 = groupList.get(j);
if (vo1.getSbid().equals(vo2.getSbid())) {
continue;
}
int count = 1;
List<VehDeviceVO> deviceList = new ArrayList<>();
deviceList.add(new VehDeviceVO(vo1.getSbid(), vo1.getSbmc()));
LocalDateTime firstTime = LocalDateTime.parse(vo1.getGcsj(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime lastTime = LocalDateTime.parse(vo2.getGcsj(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
for (int k = j + 1; k < groupList.size(); k++) {
AnalysisJudgmentVehicleVO vo3 = groupList.get(k);
if (vo2.getSbid().equals(vo3.getSbid())) {
if (isWithinXMinutes(vo2, vo3, x)) {
count++;
deviceList.add(new VehDeviceVO(vo3.getSbid(), vo3.getSbmc()));
lastTime = LocalDateTime.parse(vo3.getGcsj(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
} else {
break;
}
if (count >= y) {
AnalysisJudgmentVehicleWithFollowVO followVO = new AnalysisJudgmentVehicleWithFollowVO();
followVO.setHpzl(vo1.getHpzl());
followVO.setHphm(vo1.getHphm());
followVO.setBscHpzl(vo2.getHpzl());
followVO.setBscHphm(vo2.getHphm());
followVO.setBssj(String.valueOf(Duration.between(firstTime, lastTime).toMinutes()));
followVO.setDeviceList(deviceList);
followList.add(followVO);
break;
}
}
}
}
}
}
return followList;
}
原文地址: https://www.cveoy.top/t/topic/e14l 著作权归作者所有。请勿转载和采集!