您的位置:首页 > 编程语言 > Java开发

Java 使用Map对出现的字符进行计数

2017-04-06 21:24 288 查看
public class Test{
public static void main(String[] args) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
String string = "BbB I have a good friend, we met each other when I was five years old. She is my neighbor and the first time";
char[] cs = string.toCharArray();
for (char c : cs) {
map.put(c, (map.get(c)==null?1:map.get(c)+1));//核心代码就这一段
}
System.out.println(map);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐