题解 | #字符统计#
字符统计
https://www.nowcoder.com/practice/c1f9561de1e240099bdb904765da9ad0
import java.util.*; import java.util.stream.Stream; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); ArrayList<String> arrayList = new ArrayList<>(); for (String s : str.split("")) { arrayList.add(s); } HashMap<String, Integer> HM = new HashMap<>(); for (String s : arrayList) { if (HM.get(s) == null) { HM.put(s, 1); } else { HM.put(s, HM.get(s) + 1); } } HM.keySet().stream().sorted((String o1, String o2)-> { if (HM.get(o1) - HM.get(o2) > 0) return -1; else if (HM.get(o1) - HM.get(o2) < 0) return 1; else return o1.compareTo(o2); }) .forEach(s -> System.out.print(s)); } }