public static <T> Set<String> replaceMappedRanges(List<T> values) {

    Map<T, SortedSet<Integer>> convolutedValues = new HashMap<>();

    int valueIndex = 0;

    for (T value : values) {

      SortedSet<Integer> correspValues = convolutedValues.get(value);

      if (correspValues == null) {
        correspValues = new TreeSet<>();
        convolutedValues.put(value, correspValues);
      }
      correspValues.add(valueIndex);
      valueIndex++;
    }

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

    for (Entry<T, SortedSet<Integer>> entry : convolutedValues.entrySet()) {
      Set<String> replacedRanges = replaceRanges(entry.getValue());
      result.add(entry.getKey() + ":" + Joiner.on(",").join(replacedRanges));
    }

    return result;
  }
Finds ranges in list and replaces ranges by compact notation
<p/>
<p>For example, suppose <tt>list</tt> comprises<tt> [1, 1, 2, 2, 1, 3]</tt>.
After invoking <tt>rangedMappedSet = StageUtils.replaceMappedRanges(list)</tt>
<tt>rangedMappedSet</tt> will comprise
<tt>["1:0-1,4", "2:2-3", "3:5"]</tt>..

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