题解 | #字符统计#

字符统计

https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Arrays.*;
import static java.util.stream.Stream.*;


public class Main {
    public static void main(String[] args) throws IOException {

        testTh();
    }
    private static void testTh() throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = bf.readLine()) != null) {
            char[] chars = str.toCharArray();
            //store[] stores=new store[];
            TreeMap<String, Integer>hm = new TreeMap<>();
            int num = 0;
            for (char aChar : chars) {
                String s = aChar + "";
                if (hm.containsKey(s)) {
                    Integer integer = hm.get(s);
                    integer++;
                    hm.put(s, integer);
                } else {
                    hm.put(s, 1);
                }
            }
            Set<Map.Entry<String, Integer>> entries = hm.entrySet();
            List<Map.Entry<String, Integer>> list = new
            ArrayList<Map.Entry<String, Integer>>(entries);
            Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
                @Override
                public int compare(Map.Entry<String, Integer> o1,
                                   Map.Entry<String, Integer> o2) {
                    int i = o2.getValue() - o1.getValue();
                    if (i == 0) {
                        i = o1.getKey().compareTo(o2.getKey());
                    }
                    return i;
                }
            });
            for (Map.Entry<String, Integer> entry : list) {
                System.out.print(entry.getKey());
            }

        }

    }

}

全部评论

相关推荐

不愿透露姓名的神秘牛友
01-05 08:26
牛客880205450号:有人鸽了,才有空缺
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务