问题:将一个时间序列每5秒分一组,返回嵌套的list;
原理:int除int会得到一个int(也就是损失精度)
输入:排序后的list,每几秒分组值
private static List<List<Long>> getSerialSlice(List<Long> serialList, long values) {return new ArrayList<>(serialList.stream().collect(Collectors.groupingBy(timestamp -> timestamp/ values)).values());}
测试:
public static void main(String[] args) {List<Long> list = new ArrayList<>();list.add(1800000L);list.add(1801000L);list.add(1802000L);list.add(1803000L);list.add(1804000L);list.add(1805000L);list.add(1807000L);list.add(1808000L);list.add(1810000L);list.add(1813000L);list.add(1816000L);list.add(1817000L);List<List<Long>> slice = getSerialSlice(list, 5000);for (List<Long> longs : slice) {System.out.println("longs = " + longs);}}
结果:
ps:有人就想要12345为一组,而不要01234为一组,也可以:
private static List<List<Long>> getSerialSlice(List<Long> serialList, long values) {return new ArrayList<>(serialList.stream().collect(Collectors.groupingBy(timestamp -> (timestamp-1000)/ values)).values());}