提问人:venu 提问时间:8/3/2023 最后编辑:venu 更新时间:8/5/2023 访问量:58
列表具有重叠日期范围的自定义对象,需要将所有重叠范围分解为新的周期,并在 java 中保持非重叠 AS IS
List has custom object with overlap date ranges need to break all overlap ranges into new periods and keep non overlap AS IS in java
问:
考虑下面的例子,inputList 有重叠的日期范围
inputList.add(new DateRange(LocalDate.of(2023, 7, 1), LocalDate.of(2023, 7, 10)));
inputList.add(new DateRange(LocalDate.of(2023, 7, 5), LocalDate.of(2023, 7, 30)));
inputList.add(new DateRange(LocalDate.of(2023, 7, 10), LocalDate.of(2023, 7, 20)));
inputList.add(new DateRange(LocalDate.of(2023, 7, 15), LocalDate.of(2023, 7, 20)));
inputList.add(new DateRange(LocalDate.of(2023, 8, 1), LocalDate.of(2023, 8, 20)));
需要合并所有重叠期,并在 resultList 中保留剩余的非重叠期
resultList(new DateRange(LocalDate.of(2023, 7, 1), LocalDate.of(2023, 7, 04)));
resultLis(new DateRange(LocalDate.of(2023, 7, 5), LocalDate.of(2023, 7, 09)));
resultLis(new DateRange(LocalDate.of(2023, 7, 10), LocalDate.of(2023, 7, 10)));
resultLis(new DateRange(LocalDate.of(2023, 7, 11), LocalDate.of(2023, 7, 14)));
resultLis(new DateRange(LocalDate.of(2023, 7, 15), LocalDate.of(2023, 7, 20)));
resultLis(new DateRange(LocalDate.of(2023, 7, 21), LocalDate.of(2023, 7, 30)));
resultLis(new DateRange(LocalDate.of(2023, 8, 1), LocalDate.of(2023, 8, 20)));
在重叠周期不拆分后使用以下代码
class DateRange {
LocalDate start;
LocalDate end;
public DateRange(LocalDate start, LocalDate end) {
this.start = start;
this.end = end;
}
@Override
public String toString() {
return "DateRange [start=" + start + ", end=" + end + "]";
}
}
public class Main1 {
public static void main(String[] args) {
List<DateRange> inputList = new ArrayList<>();
inputList.add(new DateRange(LocalDate.of(2023, 7, 1), LocalDate.of(2023, 7, 10)));
inputList.add(new DateRange(LocalDate.of(2023, 7, 5), LocalDate.of(2023, 7, 30)));
inputList.add(new DateRange(LocalDate.of(2023, 7, 10), LocalDate.of(2023, 7, 20)));
inputList.add(new DateRange(LocalDate.of(2023, 7, 15), LocalDate.of(2023, 7, 20)));
inputList.add(new DateRange(LocalDate.of(2023, 8, 1), LocalDate.of(2023, 8, 20)));
List<DateRange> resultList = processDateRanges(inputList);
resultList.forEach(System.out::println);
}
public static List<DateRange> processDateRanges(List<DateRange> inputList) {
List<DateRange> result = new ArrayList<>();
Collections.sort(inputList, Comparator.comparing(r -> r.start));
for (int i = 0; i < inputList.size(); i++) {
DateRange current = inputList.get(i);
LocalDate start = current.start;
LocalDate end = current.end;
for (int j = i + 1; j < inputList.size(); j++) {
DateRange next = inputList.get(j);
if (next.start.isAfter(end.plusDays(1))) break;
if (next.start.isAfter(start)) {
result.add(new DateRange(start, next.start.minusDays(1)));
}
if (next.end.isBefore(end)) {
result.add(new DateRange(next.start, next.end));
start = next.end.plusDays(1);
} else {
start = next.start;
end = next.end;
}
}
result.add(new DateRange(start, end));
while (i + 1 < inputList.size() && inputList.get(i + 1).start.isBefore(end.plusDays(1))) {
i++;
}
}
return result;
}
}
获取输出
- 2023-07-01 - 2023-07-04
- 2023-07-05 - 2023-07-09
- 2023-07-10 - 2023-07-20
- 2023-07-15 - 2023-07-20
- 2023-07-21 - 2023-07-30
- 2023-08-01 - 2023-08-20
预期如下
- 2023-07-01 - 2023-07-04 - 第一条记录重叠期前
- 2023-07-05 - 2023-07-09 - 与第1和第2记录重叠
- 2023-07-10 - 2023-07-10 - 与第1、2、3条记录重叠
- 2023-07-11 - 2023-07-14 - 与第二和第三记录重叠
- 2023-07-15 - 2023-07-20 - 与第2、3、4项记录重叠
- 2023-07-21 - 2023-07-30 - 第二条记录重叠期后
- 2023-08-01 - 2023-08-20 - 非重叠期
答: 暂无答案
评论