Package progs

Source Code of progs.IntervalOverlap

package progs;

import progs.lib.Range;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Merge intervals such that overlapping intervals are merged.
* <p/>
* author: KRishna Atkuru
*/
public class IntervalOverlap {

  public static void main(String[] args) {
    Range[] values = {new Range(5, 20), new Range(2, 7), new Range(30, 55)};
    List<Range> rangeList = new ArrayList<Range>();
    Collections.addAll(rangeList, values);
    Collections.sort(rangeList);
    List<Range> returnList = new ArrayList<Range>();
    Range previousElement = rangeList.remove(0);
    while (rangeList.size() > 0) {
      Range currentElement = rangeList.remove(0);
      if (previousElement.getHigh() >= currentElement.getLow()) {
        previousElement.setHigh(currentElement.getHigh() > previousElement.getHigh()
            ? currentElement.getHigh() : previousElement.getHigh());
      } else {
        returnList.add(new Range(previousElement));
        previousElement = currentElement;
      }
    }
    returnList.add(previousElement);
    for (Range range : returnList) {
      System.out.println(range.toString());
    }
  }
}
TOP

Related Classes of progs.IntervalOverlap

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.