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);
}
}
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);
}
}
相关文章推荐
- Java使用substring()判断指定字符出现次数
- (使用STL自带的排序函数进行编程7.3.14)UVA 10062 Tell me the frequencies!(求一个字符出现的次数(包括字母和数字)&&按出现的次数降序)
- java中用正则表达式获取一个指定的子字符在字符串中出现的指定次数的下标索引,并且进行字符串分割
- spring-data-jpa 中,如果使用了one-to-many , many-to-one的注释,会在Jackson进行json字符串化的时候出现错误
- 编译JAVA源码时出现编码GBK的不可映射字符(使用notepad++编辑器)
- java如何对map进行排序详解(map集合的使用)
- 第一个只出现一次的字符——map的使用
- cmd命令行对java程序进行编译时出现错误:编码GBK的不可映射字符
- 【java编程】Map集合之TreeMap字符串中字符出现次数
- 【Java】【TIJ】使用static实现对创建的类对象进行计数
- Java中使用Map对象进行数据分组
- 关于使用javac进行编译java文件时,出现的java.lang.NotClassDefFoundError解决
- 290. Word Pattern-leetCode(字符模式匹配)(简单,使用两组Map进行查找判断)
- java 使用map集合出现的问题
- Java使用字符流拷贝文件出现乱码
- spring-data-jpa 中,如果使用了one-to-many , many-to-one的注释,会在Jackson进行json字符串化的时候出现错误
- 计数字符串中字符出现的次数(java实现)
- 使用排序算法对数组进行排序,按升序 注:数组内容只会有大写字母,且可能会出现重复,不允许使用java.util下的任何类
- JAVA 编译出现编码GBK的不可映射字符(使用notepad++编辑器)
- java如何对map进行排序详解(map集合的使用)