运动场上有很多组照明设备,下面A集合中,描述出了不同照明设备的启动(begintime)停止(endtime)时间,照明设备的id已经处理掉了无需考虑,仔细看这些时间段可以发现,这些时间有重合(表示这段时间多组照明同时亮)、有空挡(表示这段时间一组照明都没有开)。请通过java代码实现,获得此足球场上有照明设备开启的时间段集合(求A集合在时间维度上的并集)。 A集合: [{begintime:2020-01-01 00:04:00,endtime:2020-01-01 00:06:00}, {begintime:2020-01-01 00:01:05,endtime:2020-01-01 00:03:15}, {begintime:2020-01-01 00:09:00,endtime:2020-01-01 00:10:00}, {begintime:2020-01-01 00:07:35,endtime:2020-01-01 00:08:40}, {begintime:2020-01-01 00:12:00,endtime:2020-01-01 00:17:00}, {begintime:2020-01-01 00:12:05,endtime:2020-01-01 00:13:10}, {begintime:2020-01-01 00:19:00,endtime:2020-01-01 00:21:00}, {begintime:2020-01-01 00:15:20,endtime:2020-01-01 00:20:45}] 期望得到的输出结果: [{begintime:2020-01-01 00:01:05,endtime:2020-01-01 00:03:15}, {begintime:2020-01-01 00:04:00,endtime:2020-01-01 00:06:00}, {begintime:2020-01-01 00:07:35,endtime:2020-01-01 00:08:40}, {begintime:2020-01-01 00:09:00,endtime:2020-01-01 00:10:00}, {begintime:2020-01-01 00:12:00,endtime:2020-01-01 00:21:00}]
时间: 2024-01-09 15:09:38 浏览: 83
可以使用如下的 Java 代码实现:
```
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class Main {
public static void main(String[] args) throws ParseException {
List<Map<String, String>> A = new ArrayList<>();
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:04:00"); put("endtime", "2020-01-01 00:06:00");}});
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:01:05"); put("endtime", "2020-01-01 00:03:15");}});
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:09:00"); put("endtime", "2020-01-01 00:10:00");}});
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:07:35"); put("endtime", "2020-01-01 00:08:40");}});
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:12:00"); put("endtime", "2020-01-01 00:17:00");}});
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:12:05"); put("endtime", "2020-01-01 00:13:10");}});
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:19:00"); put("endtime", "2020-01-01 00:21:00");}});
A.add(new HashMap<String, String>() {{put("begintime", "2020-01-01 00:15:20"); put("endtime", "2020-01-01 00:20:45");}});
List<Map<String, String>> result = mergeTime(A);
System.out.println(result);
}
public static List<Map<String, String>> mergeTime(List<Map<String, String>> A) throws ParseException {
List<Map<String, String>> result = new ArrayList<>();
if (A == null || A.size() == 0) {
return result;
}
// 按照起始时间排序
Collections.sort(A, new Comparator<Map<String, String>>() {
public int compare(Map<String, String> o1, Map<String, String> o2) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date beginTime1 = sdf.parse(o1.get("begintime"));
Date beginTime2 = sdf.parse(o2.get("begintime"));
return beginTime1.compareTo(beginTime2);
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
});
String beginTime = A.get(0).get("begintime");
String endTime = A.get(0).get("endtime");
for (int i = 1; i < A.size(); i++) {
String curBeginTime = A.get(i).get("begintime");
String curEndTime = A.get(i).get("endtime");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date endTimeDate = sdf.parse(endTime);
Date curBeginTimeDate = sdf.parse(curBeginTime);
if (endTimeDate.compareTo(curBeginTimeDate) >= 0) { // 有重合
endTime = curEndTime.compareTo(endTime) > 0 ? curEndTime : endTime;
} else { // 无重合
result.add(new HashMap<String, String>() {{put("begintime", beginTime); put("endtime", endTime);}});
beginTime = curBeginTime;
endTime = curEndTime;
}
}
result.add(new HashMap<String, String>() {{put("begintime", beginTime); put("endtime", endTime);}});
return result;
}
}
```
在程序中,首先按照起始时间对照明设备进行排序。然后遍历整个集合,对于当前照明设备,如果其起始时间与前一个照明设备的结束时间有重合,那么将它们合并为一个时间段;否则将前一个时间段加入结果集合中,并开始新的时间段。最后将最后一个时间段加入结果集合中即可。
阅读全文