public static List<Pair<Long, Double>> sorted(
      List<Pair<Long, Double>> input, int inputSize, int desiredBuckets) {
    List<Pair<Long, Double>> results = new ArrayList<>();

    OnePassBucketizer.bucketize(input, inputSize, desiredBuckets).stream()
        .collect(new SlidingCollector<>(3, 1))
        .stream()
        .map(Triangle::of)
        .forEach(
            triangle -> {
              if (results.isEmpty()) results.add(triangle.getFirst());

              results.add(triangle.getResult());

              if (results.size() == desiredBuckets + 1) results.add(triangle.getLast());
            });

    return results;
  }
this class is copied and modified from <a
href="https://github.com/ggalmazor/lt_downsampling_java8">...</a> project
library-udf/src/main/java/org/apache/iotdb/library/dprofile/util/LTThreeBuckets.java