java 47 lines · 8 steps

Detecting calendar conflicts in Java

A sweep-line pass over time-sorted events finds every overlapping pair without comparing all of them.

Explained by highlit
1public record TimeSlot(LocalDate day, LocalDateTime start, LocalDateTime end) {}
2 
3public class ScheduleAnalyzer {
4 
5 public record Conflict(CalendarEvent first, CalendarEvent second) {}
6 
7 public Map<LocalDate, List<CalendarEvent>> groupByDay(List<CalendarEvent> events) {
8 return events.stream()
9 .collect(Collectors.groupingBy(
10 e -> e.start().toLocalDate(),
11 TreeMap::new,
12 Collectors.toList()));
13 }
14 
15 public List<Conflict> findConflicts(List<CalendarEvent> events) {
16 List<CalendarEvent> sorted = events.stream()
17 .sorted(Comparator.comparing(CalendarEvent::start))
18 .toList();
19 
20 List<Conflict> conflicts = new ArrayList<>();
21 for (int i = 0; i < sorted.size(); i++) {
22 CalendarEvent current = sorted.get(i);
23 for (int j = i + 1; j < sorted.size(); j++) {
24 CalendarEvent next = sorted.get(j);
25 if (!next.start().isBefore(current.end())) {
26 break;
27 }
28 if (overlaps(current, next)) {
29 conflicts.add(new Conflict(current, next));
30 }
31 }
32 }
33 return conflicts;
34 }
35 
36 public Map<LocalDate, List<Conflict>> conflictsByDay(List<CalendarEvent> events) {
37 return findConflicts(events).stream()
38 .collect(Collectors.groupingBy(
39 c -> c.first().start().toLocalDate(),
40 TreeMap::new,
41 Collectors.toList()));
42 }
43 
44 private boolean overlaps(CalendarEvent a, CalendarEvent b) {
45 return a.start().isBefore(b.end()) && b.start().isBefore(a.end());
46 }
47}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Sorting events by start time lets you stop scanning as soon as the next event begins after the current one ends.
  2. 2Records give you concise, immutable value types for domain concepts like a scheduling conflict.
  3. 3Passing TreeMap::new to groupingBy keeps the resulting map ordered by key for free.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Detecting calendar conflicts in Java — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code