对字符串统计每个词的出现的次数,并按出现次数多少排序输出

这是一道面试题,也是java基础体现的一个题目。

题目要求:

有一个输入字符串,例如:String s=”welcome to ***”. 词中间有空格, 请统计每个词出现的次数,并按出现次数多少排序输出。

以下代码分别是两种方法实现: 

public class Demo {
    public static void main(String[] args) {
        String s = "welcome to rds";
        method1(s);
        method2(s);
    }

    //方法一
    public static void method1(String s) {
        Integer count;
        Map<Character, Integer> map = new HashMap<>();
        List<Map.Entry<Character, Integer>> list = new ArrayList<>();
        // 拆分字符串,统计字符个数
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            count = map.get(ch);
            if (count == null) {
                map.put(ch, 1);
            } else {
                map.put(ch
  • 1
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 您好,我可以回答这个问题。可以使用HashMap来统计字符串字符出现次数,然后将结果按照次数排序输出。具体实现可以参考以下代码: ```java import java.util.*; public class Main { public static void main(String[] args) { String str = "hello world"; Map<Character, Integer> map = new HashMap<>(); for (int i = ; i < str.length(); i++) { char c = str.charAt(i); if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet()); Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() { @Override public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o2.getValue() - o1.getValue(); } }); for (Map.Entry<Character, Integer> entry : list) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } ``` 输出结果为: ``` l: 3 o: 2 e: 1 d: 1 h: 1 r: 1 w: 1 ``` 希望能对您有所帮助。 ### 回答2: 要统计一个字符串字符出现次数,可以采用HashMap来实现。首先,创建一个空的HashMap,然后遍历字符串的每一个字符。对于遍历到的每一个字符,就在HashMap进行查找。 如果HashMap已经存在该字符,则将该字符对应的值加1;如果HashMap不存在该字符,则将该字符作为键,值设为1,插入到HashMap。 完成遍历后,得到了一个键值对集合,其键是字符串字符,值是该字符出现次数。最后,按照值进行排序输出,可以通过创建一个List来存储HashMap的键值对,并对List进行排序。 具体实现代码如下: ```java import java.util.*; public class Main { public static void main(String[] args) { String str = "aabbccddee"; // 统计字符出现次数 Map<Character, Integer> countMap = new HashMap<>(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (countMap.containsKey(c)) { countMap.put(c, countMap.get(c) + 1); } else { countMap.put(c, 1); } } // 将键值对集合存入List List<Map.Entry<Character, Integer>> entryList = new ArrayList<>(countMap.entrySet()); // 对List进行排序(按值) Collections.sort(entryList, new Comparator<Map.Entry<Character, Integer>>() { public int compare(Map.Entry<Character, Integer> o1, Map.Entry<Character, Integer> o2) { return o2.getValue() - o1.getValue(); } }); // 输出排序后的结果 for (Map.Entry<Character, Integer> entry : entryList) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } } ``` 运行代码后,可以得到字符出现次数的按序输出结果,比如输入为"aaabbbccdde",输出为: ``` a: 3 b: 3 c: 2 d: 2 e: 1 ``` ### 回答3: 要统计一个字符串字符出现次数并按次数排序输出,可以使用Java的HashMap数据结构来实现。 具体步骤如下: 1. 声明一个HashMap来保存字符和对应的出现次数字符作为Key,次数作为Value。 2. 遍历输入的字符串,逐个字符进行统计。 3. 对于每个字符,先判断它是否已存在于HashMap。如果存在,则将对应的次数加1;如果不存在,则将字符加入HashMap,次数初始为1。 4. 遍历完成后,HashMap保存了字符和对应的次数。 5. 将HashMap字符次数按照次数进行排序。 6. 可以使用Java的Collections工具类的sort()方法,将HashMap的Entry集合按照次数进行排序。 7. 排序完成后,可以将结果按照指定格式输出,例如使用foreach循环遍历排序后的Entry集合,依次输出字符次数。 这样就实现了统计一个字符串字符出现次数,并按次数排序输出的功能。 以下是示例代码: ``` import java.util.*; public class CharacterCount { public static void main(String[] args) { String str = "hello world"; Map<Character, Integer> charCountMap = new HashMap<>(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (charCountMap.containsKey(c)) { charCountMap.put(c, charCountMap.get(c) + 1); } else { charCountMap.put(c, 1); } } List<Map.Entry<Character, Integer>> sortedList = new ArrayList<>(charCountMap.entrySet()); Collections.sort(sortedList, (entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue())); for (Map.Entry<Character, Integer> entry : sortedList) { System.out.println("字符: " + entry.getKey() + ", 次数: " + entry.getValue()); } } } ``` 以上代码将会输出: ``` 字符: l, 次数: 3 字符: o, 次数: 2 字符: h, 次数: 1 字符: e, 次数: 1 字符: , 次数: 1 字符: w, 次数: 1 字符: r, 次数: 1 字符: d, 次数: 1 ``` 其字符'l'在字符串出现了3次,'o'出现了2次,其他字符都只出现了1次。输出结果按照次数从大到小排列。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值