基于Lambda表达式和分组统计的伴随车识别算法实现
基于Lambda表达式和分组统计的伴随车识别算法实现
本文介绍一种基于Lambda表达式和分组统计的伴随车识别算法,该算法能够根据过车记录,快速识别出伴随车辆,并保存其相关信息。代码示例使用Java语言实现,并提供详细的注释,方便理解和应用。
算法描述
输入:
- 已排序的过车记录集合
List<AnalysisJudgmentVehicleVO>,其中每个元素包含以下属性:sbid: 设备编号sbmc: 设备名称hpzl: 车辆类型hphm: 车牌号码gcsj: 过车时间
- 阈值参数:
X: 时间间隔阈值,单位:分钟Y: 连续次数阈值
输出:
- 伴随车辆列表
List<CompanionVehicleVO>,每个元素包含以下属性:hpzl: 主车类型hphm: 主车牌号码bscHpzl: 伴随车类型bscHphm: 伴随车牌号码bssj: 伴随时间,单位:分钟VehDeviceList: 设备列表,包含主车和伴随车经过的设备信息
算法步骤:
-
分组统计: 使用Lambda表达式将过车记录按照
hpzl+hphm进行分组,得到一个Map<String, List<AnalysisJudgmentVehicleVO>>,其中键为车辆标识(hpzl+hphm),值为该车辆的过车记录列表。 -
伴随车识别: 对分组后的过车记录进行遍历,对于每辆车,依次与其他车辆进行比较,判断是否满足以下条件:
- 该车辆与其他车辆连续
Y次通过相同的设备(sbid)。 - 每次过车时间间隔小于
X分钟(通过gcsj判断)。 - 如果满足以上条件,则视为伴随车。
- 伴随车辆信息保存: 将主车和伴随车的信息保存到一个新的
CompanionVehicleVO对象中,包括车辆类型、车牌号码、伴随时间以及经过的设备列表。
代码实现
public class AnalysisJudgmentVehicleUtil {
/**
* 对过车记录进行分组
* @param vehicleList 过车记录集合
* @return Map<String, List<AnalysisJudgmentVehicleVO>> 分组后的过车记录Map
*/
public static Map<String, List<AnalysisJudgmentVehicleVO>> groupByHpzlAndHphm(List<AnalysisJudgmentVehicleVO> vehicleList) {
Map<String, List<AnalysisJudgmentVehicleVO>> vehicleMap = vehicleList.stream().collect(Collectors.groupingBy(vehicle -> vehicle.getHpzl() + vehicle.getHphm()));
return vehicleMap;
}
/**
* 对分组后的过车记录进行计算,得到伴随车辆列表
* @param vehicleMap 分组后的过车记录Map
* @param X 时间间隔阈值,单位:分钟
* @param Y 连续次数阈值
* @return List<CompanionVehicleVO> 伴随车辆列表
*/
public static List<CompanionVehicleVO> getCompanionVehicleList(Map<String, List<AnalysisJudgmentVehicleVO>> vehicleMap, int X, int Y) {
List<CompanionVehicleVO> companionVehicleList = new ArrayList<>();
for (Map.Entry<String, List<AnalysisJudgmentVehicleVO>> entry : vehicleMap.entrySet()) {
List<AnalysisJudgmentVehicleVO> vehicleList = entry.getValue();
int vehicleListSize = vehicleList.size();
for (int i = 0; i < vehicleListSize; i++) {
AnalysisJudgmentVehicleVO vehicle = vehicleList.get(i);
for (int j = i + 1; j < vehicleListSize; j++) {
AnalysisJudgmentVehicleVO companionVehicle = vehicleList.get(j);
if (vehicle.getSbid().equals(companionVehicle.getSbid())) {
continue; // 同一设备不处理
}
int gcsjInterval = DateUtil.getMinuteInterval(vehicle.getGcsj(), companionVehicle.getGcsj());
if (gcsjInterval > X) {
break; // 时间间隔大于X不再处理
}
int sameSbidCount = 1;
for (int k = j + 1; k < vehicleListSize; k++) {
AnalysisJudgmentVehicleVO tempVehicle = vehicleList.get(k);
if (tempVehicle.getSbid().equals(vehicle.getSbid())) {
break; // 设备不同不再处理
}
int tempInterval = DateUtil.getMinuteInterval(companionVehicle.getGcsj(), tempVehicle.getGcsj());
if (tempInterval > X) {
break; // 时间间隔大于X不再处理
}
sameSbidCount++;
if (sameSbidCount >= Y) {
// 符合条件,保存伴随车辆信息
CompanionVehicleVO companionVehicleVO = new CompanionVehicleVO();
companionVehicleVO.setHpzl(vehicle.getHpzl());
companionVehicleVO.setHphm(vehicle.getHphm());
companionVehicleVO.setBscHpzl(companionVehicle.getHpzl());
companionVehicleVO.setBscHphm(companionVehicle.getHphm());
companionVehicleVO.setBssj(DateUtil.getMinuteInterval(companionVehicle.getGcsj(), tempVehicle.getGcsj()) + '');
List<VehDeviceVO> vehDeviceList = new ArrayList<>();
vehDeviceList.add(new VehDeviceVO(vehicle.getSbid(), vehicle.getSbmc()));
vehDeviceList.add(new VehDeviceVO(companionVehicle.getSbid(), companionVehicle.getSbmc()));
companionVehicleVO.setVehDeviceList(vehDeviceList);
companionVehicleList.add(companionVehicleVO);
break;
}
}
}
}
}
return companionVehicleList;
}
}
总结
本文介绍了一种基于Lambda表达式和分组统计的伴随车识别算法,并提供了详细的代码实现。该算法能够高效地识别出伴随车辆,并保存其相关信息,可用于交通管理、车辆追踪等场景。
原文地址: http://www.cveoy.top/t/topic/n1fS 著作权归作者所有。请勿转载和采集!