AnIn1
AnIn2
AnIn3
AnIn4
AnIn5
.。。。。。。。。。。。。。。。
一直到AnIn5000多个字符串,现在的数据是有重复的并且乱序,我试过用String的split方式分割,但是无法去重
1.随机数构造list
Random r = new Random(1);
List<String> list = new ArrayList<>();
for (int i = 0; i < 500; i++) {
list.add("AnIn" + r.nextInt(100));
}
2.jdk1.8 stream 先去重,再对每个值进行分割得到数字并排序
list = list.stream().distinct().sorted(Comparator.comparing(value -> Integer.parseInt(value.split("AnIn")[1]))).collect(Collectors.toList());
3.查看输出结果
list.forEach(System.out::println);
你试试用HashSet
public class Test {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 500; i++) {
list.add("AnIn" + i);
}
Collections.shuffle(list);
TreeSet<String> treeSet = new TreeSet<>((s1, s2) -> {
int num1 = getNumber(s1);
int num2 = getNumber(s2);
return num1 - num2;
});
treeSet.addAll(list);
System.out.println(treeSet);
}
private static int getNumber(String str) {
int num = 0;
for (int i = str.length() - 1, base = 1; i >= 0; i --, base = base * 10) {
char ch = str.charAt(i);
if (ch >= '0' && ch <= '9') {
num += (ch - '0') * base;
continue;
}
break;
}
return num;
}
}