Map<Long, BehaviorHistoryInfo> behaviorHistoryInfos = new LinkedHashMap<Long, BehaviorHistoryInfo>();
List<TableHistoryStatDO> tableHistoryStatDOs = tableHistoryStatDao.listTimelineTableStat(condition);
int size = tableHistoryStatDOs.size();
int k = size - 1;
for (Long i = condition.getStart().getTime(); i <= condition.getEnd().getTime(); i += 60 * 1000) {
BehaviorHistoryInfo behaviorHistoryInfo = new BehaviorHistoryInfo();
List<TableStat> tableStat = new ArrayList<TableStat>();
// 取出每个时间点i以内的数据,k是一个游标,每次遍历时前面已经取过了的数据就不用再遍历了
for (int j = k; j >= 0; --j) {
if ((i - tableHistoryStatDOs.get(j).getEndTime().getTime() <= 60 * 1000)
&& (i - tableHistoryStatDOs.get(j).getEndTime().getTime() >= 0)) {
tableStat.add(tableHistoryStatDOToModel(tableHistoryStatDOs.get(j)));
k = j - 1;
}// 如果不满足if条件,则后面的数据也不用再遍历
else {
break;
}
}
if (tableStat.size() > 0) {
behaviorHistoryInfo.setItems(tableStat);
behaviorHistoryInfos.put(i, behaviorHistoryInfo);
}
}
return behaviorHistoryInfos;