@Test
public void test2() {
List<String[]> listIn = new ArrayList<>();
String[] a = {"20230101", "20230105"};
String[] b = {"20230104", "20230108"};
String[] c = {"20230111", "20230120"};
listIn.add(c);
listIn.add(a);
listIn.add(b);
List<String[]> fib = merge(listIn);
for (String[] item : fib) {
log.info(Arrays.stream(item).collect(Collectors.joining(",")));
}
}
/**
* 区间合并
*
* @param listIn
* @return
*/
public static List<String[]> merge(List<String[]> listIn) {
Collections.sort(listIn, (o1, o2) -> o1[0].compareTo(o2[0]));
List<String[]> listOut = new ArrayList<>();
String begin = "";
String end = "";
for (int i = 0, size = listIn.size(); i < size; i++) {
String[] tmp = listIn.get(i);
if (i == 0) {
begin = tmp[0];
end = tmp[1];
continue;
}
if (tmp[0].compareTo(end) <= 0 && tmp[1].compareTo(end) > 0) {
end = tmp[1];
} else {
String[] str = {begin, end};
listOut.add(str);
begin = tmp[0];
end = tmp[1];
}
if (i == (size - 1)) {
String[] str = {begin, end};
listOut.add(str);
}
}
return listOut;
}
网友评论