黑马程序员技术交流社区

标题: 利用map集合计算字符串中各个字符出现的次数 [打印本页]

作者: Itheima_he    时间: 2017-3-20 23:52
标题: 利用map集合计算字符串中各个字符出现的次数
public Map<String, Integer> getCount(String str){
                Map<String, Integer> map = new HashMap<String, Integer>();
                if(!str.isEmpty()){
                        for(int i=0;i<str.length();i++){
                                String key = str.charAt(i)+"";
                                if(map.containsKey(key)){
                                        int init = map.get(key);
                                        map.put(key, init+=1);
                                }else{
                                        map.put(key, 1);
                                }
                        }
                }
               
               
               
                return map;
        }
作者: 名字不太重要    时间: 2017-3-21 00:51
厉害了,

作者: 该用户名已注册    时间: 2017-3-21 02:06
面试的时候遇到这道题了




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2