提交 b91491b4 authored 作者: 邓砥奕's avatar 邓砥奕

更新

上级 1b80103c
...@@ -19,12 +19,12 @@ public class DeviceSeqUtil { ...@@ -19,12 +19,12 @@ public class DeviceSeqUtil {
public static void main(String[] args) { public static void main(String[] args) {
List<String> list=new ArrayList<>(); List<String> list=new ArrayList<>();
list.add("a2"); list.add("a2");
list.add("a4"); list.add("c4");
list.add("a6"); list.add("d6");
list.add("a5"); list.add("c5");
list.add("a3"); list.add("a3");
list.add("a1"); list.add("a1");
compositionInterval(list); System.out.println(getContinuousSeqs(list));
} }
/** /**
...@@ -234,12 +234,66 @@ public class DeviceSeqUtil { ...@@ -234,12 +234,66 @@ public class DeviceSeqUtil {
* @return 序列号连续组合的区间 * @return 序列号连续组合的区间
*/ */
public static List<String> getContinuousSeqs(List<String> strings){ public static List<String> getContinuousSeqs(List<String> strings){
List<String> results = new ArrayList<>();
List<String> sortedString = strings.stream().sorted().collect(Collectors.toList()); List<String> sortedString = strings.stream().sorted().collect(Collectors.toList());
String lastSeq = sortedString.get(0); String lastSeq = sortedString.get(0);
for (int i = 0 ;i<sortedString.size();i++){ String first = null;
for (int i = 1 ;i<sortedString.size();i++){
String index = sortedString.get(i);
if (getFirstString(lastSeq).equals(getFirstString(index))) {
Long num1 = getLastNum(lastSeq);
Long num2 = getLastNum(index);
if (num2 == num1 + 1) {
if (first==null){
first = lastSeq;
}
if (i==sortedString.size()-1){
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(first).append("-").append(lastSeq);
results.add(stringBuffer.toString());
first =null;
}
} else {
if (first==null) {
results.add(lastSeq);
}
else {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(first).append("-").append(lastSeq);
results.add(stringBuffer.toString());
first =null;
}
}
}
else {
if (first==null) {
results.add(lastSeq);
}
else {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(first).append("-").append(lastSeq);
results.add(stringBuffer.toString());
first =null;
}
}
if (i==sortedString.size()-1){
results.add(index);
}
lastSeq = index;
}
return results;
}
/**
* 获取字符串最后的数字
*/
private static Long getLastNum(String s){
return Long.parseLong(s.replaceAll(".*[^\\d](?=(\\d+))", ""));
} }
return null;
private static String getFirstString(String s){
String s1 = s.replaceAll(".*[^\\d](?=(\\d+))", "");
return s.substring(0, s.length()-s1.length());
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论