首页 > 解决方案 > 一旦范围已经存在,如何显示错误?

问题描述

我有以下代码

    HashMap<BigDecimal, BigDecimal> allData = new HashMap<>();
    allData.put(new BigDecimal(1.00),new BigDecimal(100.00));
    allData.put(new BigDecimal(101.00),new BigDecimal(200.00));
    allData.put(new BigDecimal(201.00),new BigDecimal(300.00));
    allData.put(new BigDecimal(111.00),new BigDecimal(150.00));
    allData.put(new BigDecimal(301.00),new BigDecimal(400.00));
    allData.put(new BigDecimal(401.00),new BigDecimal(500.00));
    allData.put(new BigDecimal(210.00),new BigDecimal(220.00));
    allData.put(new BigDecimal(501.00),new BigDecimal(600.00));

这里

 allData.put(new BigDecimal(111.00),new BigDecimal(150.00));
 allData.put(new BigDecimal(210.00),new BigDecimal(220.00));

如果用户输入了所有数据,那么以上2个将无效,因为该范围已经存在。

如何检查?

标签: java

解决方案


用于存储用HashMap例的范围似乎不是理想的方式。

在您的实现中,如果新范围与现有范围具有相同的下限,它将覆盖现有条目。

以下示例的输出

HashMap<BigDecimal, BigDecimal> allData = new HashMap<>();
allData.put(new BigDecimal(1.00),new BigDecimal(100.00));
allData.put(new BigDecimal(1.00),new BigDecimal(200.00));

System.out.println(allData);

{1=200}

如您所见,第二个put覆盖了第一个条目。

如果您想保留您的HashMap实现,您可以在执行以下操作之前执行此检查,如果新条目与现有条目重叠put

    HashMap<BigDecimal, BigDecimal> allData = new HashMap<>();
    allData.put(new BigDecimal(1.00),new BigDecimal(100.00));
    allData.put(new BigDecimal(101.00),new BigDecimal(200.00));

    BigDecimal lowerBound = new BigDecimal(111.00);
    BigDecimal upperBound = new BigDecimal(150.00);

    //check if lowerBound or upperBound is between any existing entry
    boolean isOverlapping = allData.entrySet().stream()
        .anyMatch(e -> (e.getKey().compareTo(lowerBound) <= 0 && e.getValue().compareTo(lowerBound) >= 0)
            || e.getKey().compareTo(upperBound) <= 0 && e.getValue().compareTo(upperBound) >= 0);

    if(isOverlapping) {
      System.out.println("is overlapping");
    } else {
      allData.put(lowerBound, upperBound);
    }

替代方法,使用自定义类

最好为您的范围使用自定义类,该类实现一种方法,该方法检查一个范围是否与另一个重叠。它可能看起来像这样:

public class Range {
  private BigDecimal lowerBound;
  private BigDecimal upperBound;

  public Range(BigDecimal lowerBound, BigDecimal upperBound) {
    this.lowerBound = lowerBound;
    this.upperBound = upperBound;
  }

  public BigDecimal getLowerBound() {
    return lowerBound;
  }

  public BigDecimal getUpperBound() {
    return upperBound;
  }


  public boolean overlapsWith(Range other) {
    return (lowerBound.compareTo(other.lowerBound) >= 0 && upperBound.compareTo(other.lowerBound) <= 0)
        || (lowerBound.compareTo(other.upperBound) >= 0 && upperBound.compareTo(other.upperBound) <= 0);
  }
}

使用 aList存储范围值时,您可以简单地使用该anyMatch方法检查是否存在重叠范围,如下例所示:

    List<Range> allData = new ArrayList<>();
    allData.add(new Range(new BigDecimal(111.00), new BigDecimal(150.00)));

    Range anotherRange = new Range(new BigDecimal(111.00), new BigDecimal(150.00));

    boolean isOverlapping = allData.stream().anyMatch(range -> range.overlapsWith(anotherRange));

    if(isOverlapping) {
      System.out.println("already exists");
    } else {
      allData.add(anotherRange);
      System.out.println("added");
    }

推荐阅读