Java代码实现车辆伴随关系分析:分组、比较和计算
Java代码实现车辆伴随关系分析:分组、比较和计算
本代码根据已有的过车记录集合,实现车辆伴随关系分析。
输入:
- 过车记录集合List
,包含属性:sbid, sbmc, hpzl, hphm, gcsj
输出:
- 新集合List
,包含属性:hpzl, hphm, bscHpzl, bscHphm, bssj, List
算法步骤:
- **分组:**将过车记录按照hpzl+hphm进行分组。
- **比较:**对每个分组中的车辆,与其他车辆进行比较,判断是否满足伴随车条件:
- 连续Y次通过相同的设备(sbid)
- 每次过车时间间隔小于X分钟
- **计算:**如果满足伴随车条件,则将该车辆与伴随车辆的信息保存到新对象中,并计算伴随时间。
代码实现:
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;
}
}
代码说明:
groupByHpzlHphm()方法使用流操作对过车记录进行分组。isWithinXMinutes()方法判断两个过车记录之间的时间间隔是否小于X分钟。calculateFollowVehicles()方法进行伴随车计算:- 遍历分组后的过车记录,两两比较判断是否满足伴随车条件。
- 如果满足伴随车条件,则将该车辆与伴随车辆信息保存到新对象中,并计算伴随时间。
使用示例:
// 初始化过车记录集合
List<AnalysisJudgmentVehicleVO> vehicleList = ...;
// 创建AnalysisJudgmentVehicle对象
AnalysisJudgmentVehicle analysis = new AnalysisJudgmentVehicle();
// 设置时间间隔和连续通过次数
int x = 5; // 时间间隔,单位为分钟
int y = 3; // 连续通过相同设备的次数
// 分组并计算伴随车辆
Map<String, List<AnalysisJudgmentVehicleVO>> groupedVehicles = analysis.groupByHpzlHphm(vehicleList);
List<AnalysisJudgmentVehicleWithFollowVO> followVehicles = analysis.calculateFollowVehicles(groupedVehicles, x, y);
// 打印结果
System.out.println("伴随车辆列表:");
for (AnalysisJudgmentVehicleWithFollowVO followVehicle : followVehicles) {
System.out.println(followVehicle.toString());
}
注意:
- 代码中使用了Java 8的流操作和LocalDateTime类,请确保您的环境支持这些特性。
- 代码中的
AnalysisJudgmentVehicleVO、VehDeviceVO和AnalysisJudgmentVehicleWithFollowVO类需要根据实际情况进行定义。 - 代码中的
x和y参数需要根据实际需求进行调整。
原文地址: https://www.cveoy.top/t/topic/n1dB 著作权归作者所有。请勿转载和采集!