public static Set<String> replaceRanges(SortedSet<Integer> set) {

    if (set == null) {
      return null;
    }

    Set<String> rangedSet = new HashSet<>();

    Integer prevElement = null;
    Integer startOfRange = set.first();

    for (Integer i : set) {
      if (prevElement != null && (i - prevElement) > 1) {
        String rangeItem = getRangedItem(startOfRange, prevElement);
        rangedSet.add(rangeItem);
        startOfRange = i;
      }
      prevElement = i;
    }

    rangedSet.add(getRangedItem(startOfRange, prevElement));

    return rangedSet;
  }
Finds ranges in sorted set and replaces ranges by compact notation
<p/>
<p>For example, suppose <tt>set</tt> comprises<tt> [1, 2, 3, 4, 7]</tt>.
After invoking <tt>rangedSet = StageUtils.replaceRanges(set)</tt>
<tt>rangedSet</tt> will comprise
<tt>["1-4", "7"]</tt>..

@param set the source set to be ranged
ambari-server/src/main/java/org/apache/ambari/server/utils/StageUtils.java