A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

想不通为何输出字符都是null
package com.heima.example;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Scanner;
import java.util.TreeMap;

public class 排序 {

        public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("请输入一个字符串");
                String s = sc.nextLine();
                char[] arr = s.toCharArray();
                TreeMap<Integer,Character> tm = new TreeMap<>(new Comparator<Integer>() {

                        @Override
                        public int compare(Integer o1, Integer o2) {
                                int num = o2.compareTo(o1);
                                return num == 0 ? 1 : num;
                        }
                });
                HashMap<Character, Integer> hm = new HashMap<>();
                for (char c : arr) {
                        hm.put(c, !hm.containsKey(c) ? 1 : hm.get(c) + 1);
                }
                for (Character ch : hm.keySet()) {
                        tm.put(hm.get(ch), ch);
                        //System.out.println(ch + "\t:\t" + hm.get(ch));
                }
                for (Integer a : tm.keySet()) {
                        System.out.println(tm.get(a) + "\t:\t" + a);
                }
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马