判断一系列区间值比喻 0-20 10-8 21-100 ...等等 这些区间有没有重叠的方法:
java
/**
* @Author Minco
* @Date 15:44 2024-07-01
* @Description 区间范围
*//***/
public class Interval implements Comparable<Interval> {
double start;
double end;
public Interval(double start, double end) {
this.start = start;
this.end = end;
}
public double getStart() {
return start;
}
public double getEnd() {
return end;
}
@Override
public int compareTo(Interval other) {
return Double.compare(this.getStart(), other.getStart());
}
}
java
import java.util.List;
public class IntervalOverlapChecker {
public static boolean hasOverlap(List<Interval> intervals) {
if (intervals == null || intervals.size() < 2) {
return false;
}
// 首先对区间按起始位置进行排序
intervals.sort(null);
for (int i = 1; i < intervals.size(); i++) {
// 如果当前区间的起始位置小于前一个区间的结束位置,则说明有重叠
if (intervals.get(i).getStart() < intervals.get(i - 1).getEnd()) {
return true;
}
}
return false;
}
}
使用:
java
List<SpokeSetmx> mxList=spokeSet.getMxList();
List<Interval> intervals = new ArrayList<>(); //校验最小、最大
for(SpokeSetmx mx:mxList){
mx.setTenantId(tenantId);
intervals.add(new Interval(mx.getMoney1(), mx.getMoney2()));
}
if(hasOverlap(intervals)) throw new BusExcep("消费范围阶梯存在重叠");