给定一个字符串列表List<String> strList, 统计里面每一个字符串的出现次数。 如: {"aa", "aa", "b"} 输出: {"aa", 2}, {"b", 1} 补充完整下面的方法: 【杭州多测师_王sir】【杭州多测师】

复制代码
给定一个字符串列表List<String> strList, 统计里面每一个字符串的出现次数。

如:

{"aa", "aa", "b"}

输出:

{"aa", 2}, {"b", 1}

补充完整下面的方法:
public Map<String, Integer> calStringArray(List<String> strList) {
}

复制代码

 

复制代码
package baseTest;
import java.util.*;
public class Test {
    public static Map<String,Integer> calStringArray(List<String> strList){
        Map<String,Integer> reult=null;
        if(strList!=null){
            reult=new HashMap<>();
            for (String s : strList) {
                Integer count = reult.get(s);
                if(count==null){
                    count=1;
                }else{
                    count++;
                }
                reult.put(s,count);
            }
        }
        return reult;
    }
    public static void main(String[] args) {
        String[] strings = {"aa", "aa", "b"};
        Map<String,Integer> map = Test.calStringArray(Arrays.asList(strings));
        for (Map.Entry<String, Integer> entry:map.entrySet()){
            System.out.println(entry.getKey()+":"+entry.getValue()+"\t");
        }
    }
}
复制代码

 

posted @   多测师_树哥  阅读(107)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示