Here you can find the source of getConcatinatedRange(List extends Number> numbers)
public static String getConcatinatedRange(List<? extends Number> numbers)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static String getConcatinatedRange(List<? extends Number> numbers) { StringBuilder sb = new StringBuilder(); if (numbers.size() == 0) { return sb.toString(); }/*from ww w. j a va2 s . c o m*/ Number prev = null; Number current = null; for (int i = 0; i < numbers.size(); i++) { current = numbers.get(i); if (prev != null) { if (numbers.get(i - 1).intValue() == (numbers.get(i).intValue() - 1)) { continue; } else { if (sb.length() > 0) { sb.append(","); } sb.append(prev); if (prev != numbers.get(i - 1)) { sb.append("-").append(numbers.get(i - 1)); } prev = current; } } else { prev = current; } } if (sb.length() > 0) { sb.append(","); } sb.append(prev); if (prev != current) { sb.append("-").append(current); } return sb.toString(); } }