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

来源:互联网 发布:数学模型画图软件 编辑:程序博客网 时间:2024/05/29 12:25
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);
}
}
0 0
原创粉丝点击