题解 | #字符统计#
字符统计
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()); } } } }