static public List<Range> compactRanges(List<Range> ranges){
  List<Range> newRanges=new LinkedList<>();
  if (ranges.isEmpty()) {
    return newRanges;
  }
  Collections.sort(ranges);
  Range prevRange=new Range(ranges.get(0).startIndex,ranges.get(0).endIndex);
  for (int i=1; i < ranges.size(); i++) {
    Range currentRange=ranges.get(i);
    if (currentRange.startIndex == prevRange.endIndex + 1) {
      prevRange.endIndex=currentRange.endIndex;
    }
 else {
      newRanges.add(prevRange);
      prevRange=new Range(currentRange.startIndex,currentRange.endIndex);
    }
  }
  newRanges.add(prevRange);
  return newRanges;
}

static public List < Range > compactRanges ( List < Range > ranges ) { List < Range > newRanges = new LinkedList < > ( ) ; if ( ranges . isEmpty ( ) ) { return newRanges ; } Collections . sort ( ranges ) ; Range prevRange = new Range ( ranges . get ( 0 ) . startIndex , ranges . get ( 0 ) . endIndex ) ; for ( int i = 1 ; i < ranges . size ( ) ; i ++ ) { Range currentRange = ranges . get ( i ) ; if ( currentRange . startIndex == prevRange . endIndex + 1 ) { prevRange . endIndex = currentRange . endIndex ; } else { newRanges . add ( prevRange ) ; prevRange = new Range ( currentRange . startIndex , currentRange . endIndex ) ; } } newRanges . add ( prevRange ) ; return newRanges ; }
merges ranges in order to obtain a more compact representation . this is intended to work with no - overlapping ranges . behavior with overlapping ranges is undefined .
