输出解题思路及代码实现到一个类中并将代码按功能点进行拆分 每步代码上方要有注释。现已根据统计条件中的开始结束日期查出过车记录集合ListAnalysisJudgmentFollowCarVO并已按照gcsj升序排序AnalysisJudgmentFollowCarVO属性有sbidsbmchpzlhphmgcsj均为String类型。根据过车记录进行计算。每辆车与其余车辆一一进行比较hpzl+hp
//定义一个类 public class AnalysisJudgmentFollowCar { //定义常量,X和Y可以根据实际情况进行调整 private static final int X = 5; //时间间隔5分钟 private static final int Y = 3; //连续通过3次
//定义方法,根据过车记录集合计算伴随车辆
public List<FollowCarVO> getFollowCarList(List<AnalysisJudgmentFollowCarVO> recordList) {
//定义新集合,用于保存伴随车辆信息
List<FollowCarVO> followCarList = new ArrayList<>();
//遍历过车记录集合
for (int i = 0; i < recordList.size(); i++) {
AnalysisJudgmentFollowCarVO currentRecord = recordList.get(i); //获取当前过车记录
//判断当前记录是否已经与其它记录进行比较,如果是则跳过
if (currentRecord.getBscHpzl() != null && currentRecord.getBscHphm() != null) {
continue;
}
//定义变量,记录连续通过相同设备的次数
int bscs = 0;
//定义变量,记录伴随车辆的过车时间、伴随时间和连续通过的相同设备集合
String bscGcsj = null;
int bssj = 0;
List<String> sbidList = new ArrayList<>();
//遍历过车记录集合,与当前记录进行比较
for (int j = i + 1; j < recordList.size(); j++) {
AnalysisJudgmentFollowCarVO compareRecord = recordList.get(j); //获取待比较的过车记录
//如果待比较记录与当前记录车牌号和号牌种类不同,则跳过
if (!compareRecord.getHpzl().equals(currentRecord.getHpzl()) || !compareRecord.getHphm().equals(currentRecord.getHphm())) {
continue;
}
//如果待比较记录与当前记录通过的设备不同,则跳过
if (!compareRecord.getSbid().equals(currentRecord.getSbid())) {
continue;
}
//如果待比较记录的过车时间与当前记录的过车时间间隔大于X分钟,则跳过
if (bsGcsj != null && compareRecord.getGcsj().getTime() - Timestamp.valueOf(bsGcsj).getTime() > X * 60 * 1000) {
bscs = 0; //重置连续通过相同设备的次数
bscGcsj = null; //重置伴随车辆的过车时间
bssj = 0; //重置伴随时间
sbidList.clear(); //清空连续通过的相同设备集合
continue;
}
//如果待比较记录与当前记录连续通过相同设备的次数已经达到Y次,则认为是伴随车辆
bscs++; //连续通过相同设备的次数+1
//如果连续通过相同设备的次数大于等于Y,则记录伴随车辆信息
if (bscs >= Y) {
//计算伴随时间(分钟)
bssj = (int) ((compareRecord.getGcsj().getTime() - Timestamp.valueOf(bsGcsj).getTime()) / (60 * 1000));
//构建伴随车辆对象
FollowCarVO followCar = new FollowCarVO();
followCar.setHpzl(currentRecord.getHpzl());
followCar.setHphm(currentRecord.getHphm());
followCar.setBscHpzl(compareRecord.getHpzl());
followCar.setBscHphm(compareRecord.getHphm());
followCar.setBssj(bssj);
followCar.setSbidList(sbidList);
//将伴随车辆对象添加到新集合中
followCarList.add(followCar);
break; //跳出内层循环,继续处理下一条记录
}
//记录连续通过的相同设备
sbidList.add(compareRecord.getSbid());
//记录伴随车辆的过车时间
if (bsGcsj == null) {
bscGcsj = compareRecord.getGcsj().toString();
}
}
}
//返回新集合
return followCarList;
}
}
//定义伴随车辆对象
class FollowCarVO {
private String hpzl; //车牌种类
private String hphm; //号牌号码
private String bscHpzl; //伴随车牌种类
private String bscHphm; //伴随号牌号码
private int bssj; //伴随时间(分钟)
private List
//省略getter和setter方法
原文地址: http://www.cveoy.top/t/topic/e2Jr 著作权归作者所有。请勿转载和采集!