How to merge consecutive values into a range?

Sorted String İnput: 1, 2, 2A, 2B, 2C, 3, 3A, 4, 5, 6, 32C, 32D, 50, 51/1, 51/2, 60, 61, 62, 200-2E, 200-2F, 200-2G, 200-2H, 201C, 201/21P, 201/21R, 201/21S,300,300A,301-2A, 542/2K, 542/2L,583-1, 583-585D, 583-585E, 605, 605A, 605B,605C 800A.

Question is about merging consecutive values into a range. E.g. 4,5,6 are consecutive values, so range is [4-6], and 2A, 2B, 2C are consecutive values, so range is 2[A-C]. No other values can be in a range.

public static void main(String[] args) {
String input=“1, 2, 2A, 2B, 2C, 3, 3A, 4, 5, 6, 32C, 32D, 50, 51/1, 51/2, 60, 61, 62, 200-2E, 200-2F, 200-2G, 200-2H, 201C, 201/21P, 201/21R, 201/21S,300,300A,301-2A, 542/2K, 542/2L,583-1, 583-585D, 583-585E, 605, 605A, 605B,605C 800A”;
List items = Arrays.asList(input.split("\s*,\s*"));
System.out.println("items: " + items);

List merged = mergeRanges(items);
System.out.println("merged: " + merged);
}
private static List mergeRanges(List items) {
List merged = new ArrayList<>();

  int startNum = Integer.MIN_VALUE;
  int lastNum = Integer.MIN_VALUE;
  boolean inRange = false;

  for (String s : items) {
      if (isInteger(s)) {
          int currentNum = Integer.parseInt(s);

          if (!inRange) {
              inRange = true;
              startNum = currentNum;
              lastNum = currentNum;
          } else if (currentNum == lastNum + 1) {
              lastNum = currentNum;
          } else {
              addRange(merged, startNum, lastNum);
              startNum = currentNum;
              lastNum = currentNum;
          }
      } else {
          if (inRange) {
              addRange(merged, startNum, lastNum);
              inRange = false;
          }

          merged.add(s);
      }  }
   if (inRange) {
   addRange(merged, startNum, lastNum);
   }
  return merged;  }

private static boolean isInteger(String strlist) {
return strlist.matches("\d+");
}
private static void addRange(List merged, int startNum, int lastNum) {
if (lastNum - startNum >= 2) {
merged.add("[" + startNum + “-” + lastNum + “]”);
} else {
for (int i = startNum; i <=lastNum; i++) {
merged.add(Integer.toString(i));
} } }}

My code output= merged: [1, 2, 2A, 2B, 2C, 3, 3A, [4-6], 32C, 32D, 50, 51/1, 51/2, [60-62], 200-2E, 200-2F, 200-2G, 200-2H, 201C, 201/21P, 201/21R, 201/21S, 300,300A,301-2A,542/2K, 542/2L,583-1, 583-585D, 583-585E, 605, 605A, 605B,605C 800A]

for example, I sorted 4,5,6 consecutive values in the range [4-6].

I have done consecutive numbers “[]” in this format but I couldn’t make the letters. Sorted list should not be disrupted.

Expected output is to only combine ranges for the trailing alphabets. 3A “,” 4A “and” 5A numbers are consecutive but not letters.So it won’t be sorted.for example : 3A 4B 5C = [3-5][A-C]

Values that are pure numbers can be in a range, e.g. [8-11]. Values that only differ by a letter at the end can be in a range. If the trailing letters are consecutive, they must be sorted.e.g 200-2E, 200- 2F, 200-2G, 200-2H =>200 [E-H]
e.g 200-2E, 200- 2F, 200-2H => 200-2[E-F] , 200-2H. It’s broken.

“51/1” ve “51/2” “51 / [1-2]” No need to edit it.

I have to check the end of the value.

If there is a letter at the end of the value and the next value is a consecutive letter, I must include them in the range.