代码实现:字符串排序
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); } } }
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!