在给定的包含时间戳的list中,查找连续continueNum次的且时间间隔为needDiff的集合。
eg:相邻两个数据的时间戳间隔为1分钟,且超过30分钟有数据
bash
/**
*
* @param list 包含时间戳(10位)的list
* @param continueNum 至少持续的数量
* @param needDiff 相邻数据的间隔,单位:s
* @return
*/
public static List<List<TmpBean>> queryContinueList(List<TmpBean> list,int continueNum,long needDiff) {
List<List<TmpBean>> result = new ArrayList<>();
List<Long> timestamps = list.stream().map(x -> x.getPublishtime()).collect(Collectors.toList());
list.sort((x1,x2)->x1.getPublishtime().compareTo(x2.getPublishtime()));
if (timestamps.size() < continueNum) {
return null;
}
timestamps.sort(Long::compareTo); // 按照时间戳升序排序
List<List<Long>> hisList = new ArrayList<>();
long startTime = timestamps.get(0);
for (int i = 1; i < timestamps.size(); i++) {
long trueTime = startTime + needDiff * i;
long realTime = timestamps.get(i);
if (trueTime == realTime) {
continue;
} else {
List<TmpBean> delBeans = new ArrayList<>(list.subList(0, i + 1));
result.add(delBeans);
list.removeAll(delBeans);
List<Long> delTimes = new ArrayList<>(timestamps.subList(0, i + 1));//左闭右开
hisList.add(delTimes);
timestamps.removeAll(delTimes);
int nowSize = timestamps.size();
if (nowSize < continueNum) {
List<TmpBean> delLastBean = new ArrayList<>(list.subList(0,nowSize));
result.add(delLastBean);
break;
} else {
startTime = timestamps.get(0);
}
i = 0;
}
}
List<List<TmpBean>> lists = result.stream().filter(lis -> lis.size() >= continueNum).collect(Collectors.toList());
return lists;
}
public static void main(String[] args) {
//调用此方法,可以找到list中所有相邻时间为60s且连续次数>=30的目标list
queryContinueList(list,30,60);
}