判断一系列区间值比喻 0-20 10-8 21-100 ...等等 这些区间有没有重叠的方法:
/*** @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;}@Overridepublic int compareTo(Interval other) {return Double.compare(this.getStart(), other.getStart());}}
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;}
}
使用:
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("消费范围阶梯存在重叠");