import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet; //字符串排序 public class Test { public static void main(String[] args) { TreeSet<Character> ts = new TreeSet<>(new Comparator<Character>() { @Override public int compare(Character c1, Character c2) { int num = c1 -c2 ; return num == 0 ? 1 : num; } }); Scanner sc = new Scanner(System.in); System.out.println("请输入字符串:"); String s = sc.nextLine(); char[] chs = s.toCharArray(); for (char c : chs) { ts.add(c); } for (char c : ts) { System.out.print(c); } } }