public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串");
String line = sc.nextLine();
char[] arr = line.toCharArray();
TreeSet<Character> ts = new TreeSet<>(new Comparator<Character>() {
@Override
public int compare(Character c1, Character c2) {
//int num = c1 - c2; //自动拆箱
int num = c1.compareTo(c2);
return num == 0 ? 1 : num;
}
});
//4,遍历字符数组,将每一个字符存储在TreeSet集合中
for(char c : arr) {
ts.add(c); //自动装箱
}
//5,遍历TreeSet集合,打印每一个字符
for(Character c : ts) {
System.out.print(c);
}
}
|
|