问题描述:我需要对一个连续的时间戳list进行分片,分片规则是下一个数据比当前数据要大于某一个阈值则进行分片;
解决方式:
1、输入的有顺序的list ,和需要进行分片的阈值
2、调用方法,填入该排序的list和阈值
private static List<List<Long>> getObuInfoGroup(List<Long> infoGroupList, long values) {List<List<Long>> res = new ArrayList<>();List<Long> subList = new ArrayList<>();for (int i = 0; i < infoGroupList.size(); i++) {if (i > 0 && (infoGroupList.get(i) - infoGroupList.get(i - 1) > values)) {res.add(subList);subList = new ArrayList<>();}subList.add(infoGroupList.get(i));}res.add(subList);return res;}
检验:
public static void main(String[] args) {List<Long> list = new ArrayList<>();list.add(18000000L);list.add(18001000L);list.add(18002000L);list.add(18003000L);list.add(18004000L);list.add(18006000L);list.add(18007000L);list.add(18010000L);list.add(18011000L);list.add(18012000L);list.add(18015000L);list.add(18016000L);list.add(18017000L);list.add(18018000L);//这块我们的序列如果是无序的,可以 list.sort(xx)一下List<List<Long>> listList = getObuInfoGroup(list, 2000);for (List<Long> longs : listList) {System.out.println("longs = " + longs);}}
结果:(应该是成功了)