# Concise Java Solution

• ``````/**
* Definition for an interval.
* public class Interval {
*     int start;
*     int end;
*     Interval() { start = 0; end = 0; }
*     Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> merge(List<Interval> intervals) {
List<Interval> ret = new ArrayList<>();
if(intervals.size() == 0) return ret;
// Add a dummy interval to get rid of end case
// Sorting the intervals list in ascending order w.r.t start value
intervals.sort(new Comparator<Interval>(){
@Override
public int compare(Interval a, Interval b){
return a.start-b.start;
}
});
int min = 0;
int max = 0;
int i = 0;
// for each interval check if the max of the interval is greater than the min of the next
//and save the max to compare with later values.
while (i<intervals.size()-1){
min = intervals.get(i).start;
max = intervals.get(i).end;
while(i<intervals.size()-1 && max >= intervals.get(i+1).start){
max = Math.max(max, Math.max(intervals.get(i).end,intervals.get(i+1).end));
i++;
}