我有一个阈值列表,例如[(10,100),(60,75),(50,70),(20,90)]
我需要订购该列表,例如俄罗斯娃娃,最高的包含下一个,依此类推。
验证此列表的最佳方式是什么?下面的代码是我当前的方法,但是我认为它可能会更好。

public class MThresholds implements Comparable< MThresholds>{

  private Double threshold_low = null;
  private Double threshold_high = null;

  public MThresholds(Double thresholdLow, Double thresholdHigh) {
      this.threshold_low = thresholdLow;
      this.threshold_high = thresholdHigh;
  }

  @Override
  public int compareTo(MThresholds thresholds) {
    return this.getThreshold_high().compareTo(thresholds.getThreshold_high());
  }
}


public static void main(String[] args) throws BadRequestException {
        List<MThresholds> thresholdsList = new ArrayList<>();
        thresholdsList.add(new MThresholds(42.0, 48.0));
        thresholdsList.add(new MThresholds(30.0, 60.0));
        thresholdsList.add(new MThresholds(20.0, 70.0));
        thresholdsList.add(new MThresholds(40.0, 50.0));
        thresholdsList.add(new MThresholds(10.0, 80.0));

        thresholdsList.sort(Collections.reverseOrder());

        for (int index = 0; index < thresholdsList.size() - 1; index++) {
            MThresholds currentThreshold = thresholdsList.get(index);
            MThresholds nextThreshold = thresholdsList.get(index + 1);
            if (!(currentThreshold.getThreshold_high() > nextThreshold.getThreshold_high() && currentThreshold.getThreshold_low() < nextThreshold.getThreshold_low())) {
                throw new Exception("Hard threshold should contain the soft one");
            }

        }
    }


因此,我的想法是按一个字段(阈值高值)对列表进行排序,然后迭代该列表并检查当前阈值是否包含下一个。这种方法有效,但是对我来说看起来有点愚蠢。有什么办法可以改善?

最佳答案

您的代码没有什么特别愚蠢的。但是,您可以使用if语句并将其作为对象上的方法移到MThresholds类中,以检查一个阈值是否包含在另一个阈值中。

   public boolean contains(MThresholds that) {
        return that.threshold_low >= threshold_low && that.threshold_high <= threshold_high;
    }


然后,您可以修改比较器或创建一个独立的比较器,该比较器期望所有阈值都包含另一个阈值或被另一个阈值包含,并且由于阈值的任何重叠而失败。

    @Override
    public int compareTo(MThresholds thresholds) {
        if (contains(thresholds)) {
            return 1;
        }

        if (thresholds.contains(this)) {
            return -1;
        }

        throw new IllegalStateException("Thresholds overlap");
    }


将它们放在一起可能看起来像这样:

public class MThresholds {

    private static class NoIntersectionComparator implements Comparator<MThresholds> {
        @Override
        public int compare(MThresholds o1, MThresholds o2) {
            if (o1.contains(o2)) {
                return 1;
            }

            if (o2.contains(o1)) {
                return -1;
            }

            throw new IllegalStateException("Thresholds overlap");
        }
    }

    private final Double threshold_low;
    private final Double threshold_high;

    public MThresholds(Double thresholdLow, Double thresholdHigh) {
        this.threshold_low = thresholdLow;
        this.threshold_high = thresholdHigh;
    }

    public boolean contains(MThresholds that) {
        return that.threshold_low >= threshold_low && that.threshold_high <= threshold_high;
    }

    public static void main(String[] args) throws Exception {
        List<MThresholds> thresholdsList = new ArrayList<>();
        thresholdsList.add(new MThresholds(42.0, 48.0));
        thresholdsList.add(new MThresholds(30.0, 60.0));
        thresholdsList.add(new MThresholds(20.0, 70.0));
        thresholdsList.add(new MThresholds(40.0, 50.0));
        thresholdsList.add(new MThresholds(10.0, 80.0));
        thresholdsList.sort(new NoIntersectionComparator().reversed());
    }
}

10-01 09:05
查看更多