按出现次数排序

Sort by number of apearances

例如,给我一个单词,我必须按照该单词中出现的次数对它的字母进行排序,如果 2 个字母出现的次数相同,它将按词典最小值排序。 现在,我已经开始查看一个字母在一个单词中出现了多少次,但从这里我不知道具体怎么做。 这个问题需要我使用 BufferedReader 和 BufferedWriter。

import java.util.*;

public class Main {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    Map<Character, Integer> m = new HashMap<>();
    String s = sc.nextLine();
    for (int i = 0; i < s.length(); ++i) {
        char c = s.charAt(i);
        if (m.containsKey(c))
            m.put(c, m.get(c) + 1);
        else
            m.put(c, 1);
    }
    for (char letter = 'a'; letter <= 'z'; ++letter)
        if (m.containsKey(letter))
            System.out.println(letter + ": " + m.get(letter));
}

目前我发布的是单词中出现次数最多的字母,但我不知道如何按出现次数对它们进行排序,以防万一有两个字母出现次数相同最小字典序。

计算word中的字母,可以用更简单的方法:

定义包含 26 个零的数组,扫描输入行并在此数组中增加适当的索引,所以如果您遇到 'a'(或 'A' - 相同的字母,但不同的符号)-您将增加索引 0、b - 索引 1 等处的值

在此扫描期间,您还可以计算出现次数最多的符号,如下所示:

public static void main(final String[] args) throws IOException {
    char maxSymbol = 0;
    int maxCount = 0;

    final int[] counts = new int[26]; // number of times each letter (a-z) appears in string
    try (final BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
        final String s = br.readLine().toLowerCase(); // calculate case-insensitive counts
        for (final char c : s.toCharArray()) {
            final int idx = c - 'a'; // convert form ASCII code to 0-based index
            counts[idx]++;

            if (counts[idx] > maxCount) {
                maxSymbol = c; // we found most occurred symbol for the current moment
                maxCount = counts[idx];
            } else if (counts[idx] == maxCount) { // we found 2nd most occurred symbol for the current moment, need to check which one is minimal in lexicographical order
                if (c < maxSymbol) {
                    maxSymbol = c;
                }
            }
        }
    }

    if (maxSymbol > 0) {
        System.out.println("Most frequent symbol " + maxSymbol + " occurred " + maxCount);
    }
}

我已经使用缓冲 reader 从 stdin 获取数据,但我不知道将缓冲写入器放在哪里,也许是为了打印结果?

希望这就是你想要的

public static void main(String[] args) {
    Map<Character, Integer> m = new HashMap<>();
    String testString = "Instructions";
    Map<Character, List<Character>> map = new HashMap<>();

    for (int i = 0; i < testString.length(); i++) {
        char someChar = testString.charAt(i);
        if (someChar == ' ') {
            continue;
        }
        char ch = testString.charAt(i);
        List<Character> characters = map.getOrDefault(Character.toLowerCase(ch), new ArrayList<>());
        characters.add(ch);
        map.put(Character.toLowerCase(ch), characters);
    }
    List<Map.Entry<Character, List<Character>>> list = new ArrayList<>(map.entrySet());

    list.sort((o1, o2) -> {
        if (o1.getValue().size() == o2.getValue().size()) {
            return o1.getKey() - o2.getKey();/// your lexicographic comparing
        }
        return o2.getValue().size() - o1.getValue().size();
    });

    list.forEach(entry -> entry.getValue().forEach(System.out::print));
}