I have two lists, each of sets of intervals. I want to find the intersection of the two lists.
Eg.
List1 = [{1, 10}, {15, 25}, {30, 32}];
List2 = [{3,5}, {9,14}, {17,20}];
Result:
IntersectionList = [{3,5}, {9,10}, {17,20}];
I've tried using: Java algorithm for find intersection between intervals but this just has one object in the first list.
I'd like to be able to give multiple interval objects in each list.
public class CalendarTimeSlot {
private long from;
private long to;
}
public class IntersectIntervalsHelper {
public static void main(String[] args) {
List<CalendarTimeSlot> l1 = new ArrayList<>();
l1.add(new CalendarTimeSlot(1, 10));
l1.add(new CalendarTimeSlot(15, 25));
l1.add(new CalendarTimeSlot(30, 32));
List<CalendarTimeSlot> l2 = new ArrayList<>();
l2.add(new CalendarTimeSlot(3, 5));
l2.add(new CalendarTimeSlot(9, 14));
l2.add(new CalendarTimeSlot(17, 20));
//todo code here to do l1 intersection l2 slots
}
}
Sample Input:
List1 = [{1, 10}, {15, 25}, {30, 32}];
List2 = [{3,5}, {9,14}, {17,20}];
Result:
IntersectionList = [{3,5}, {9,10}, {17,20}];