MergeIntervals { public List<Interval> merge(List<Interval> intervals) { Comparator<Interval> comparator = Comparator.<Interval, Integer>comparing(i -> i.start).thenComparing(j -> j.end); Collections.sort(intervals, comparator); List<Interval> merged = new ArrayList<>(); int i = 0; while (i < intervals.size()) { Interval current = intervals.get(i); int j = i + 1; int newStart = current.start; int newEnd = current.end; while (j < intervals.size() && intervals.get(j).start <= newEnd) { newEnd = Math.max(newEnd, intervals.get(j).end); j++; } merged.add(new Interval(newStart, newEnd)); i = j; } return merged; }  List<Interval> merge(List<Interval> intervals);  }
@Test(dataProvider = "intervals") public void test(List<Interval> intervals, List<Interval> expectedMerged) { assertEquals(new MergeIntervals().merge(intervals), expectedMerged); }