Java List Concatenate getConcatinatedRange(List numbers)

Here you can find the source of getConcatinatedRange(List numbers)

Description

get Concatinated Range

License

Apache License

Declaration

public static String getConcatinatedRange(List<? extends Number> numbers) 

Method Source Code

//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();
    }
}

Related

  1. concatValues(List stringValues, boolean spaceSeparated)
  2. concatWithSeparator(List list, Object delimiter)
  3. concatWithSeparator(List missingParameters, String separator)
  4. getConcatenatedStringFromList(List input, String delimiter)
  5. getConcatenateString(List codes)
  6. getZipAndConcatenated(List input1, List input2, String delimiter)
  7. listToConcatenatedString(List stringList, String delimiter)
  8. toString(List _toString, String _concator)