TreeSet實現輸入字串排序(不去除重複字元)
阿新 • • 發佈:2019-01-10
class hello { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); char[] c = s.toCharArray(); TreeSet<Character> ts = new TreeSet<>(new Comparator<Character>() {//匿名內部類定義Comparator @Override public int compare(Character c1, Character c2) { // TODO Auto-generated method stub int result = c1.charValue() - c2.charValue(); return result == 0 ? 1 : result; } }); for (Character d : c) { ts.add(d); } for (Character character : ts) { System.out.print(" "+character); } }
編譯結果: