java - 一旦范围已经存在,如何显示错误?
问题描述
我有以下代码
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个将无效,因为该范围已经存在。
如何检查?
解决方案
用于存储用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");
}
推荐阅读
- pandas - 我怎样才能调整这个数据框索引?
- javascript - Chrome 扩展脚本在注入后不会执行
- assembly - 8086 masm,如何跳过输入对象文件名、源列表等,并使用 Notepad++ 在 DOSBox 中直接执行 asm 文件
- javascript - 来自浏览器的 Google Maps Place 请求
- c# - C# - 检查 DataAdapter 状态
- azure - 天蓝色函数 v 2.0 和密钥库
- node.js - Angular 6/Express 中的路由错误
- python-2.7 - 如何使用 python 下载谷歌电子表格?
- string - 搜索以小写开头后跟大写的字符串
- glsl - glsl 有一个错误:无法从“highp float”转换为“highp 3-component vector of float”