📜  地图 - 用地图计数 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:23.393000             🧑  作者: Mango

代码示例1
// counting how many letters int the string
String s = "alma mater";
Map countOfLetters = new HashMap<>();
for (char c: s.toCharArray()) {
    if (countOfLetters.containsKey(c)) {
        countOfLetters.put(c, countOfLetters.get(c) + 1);
    }
    else {
        countOfLetters.put(c, 1);
    }
}
System.out.println(countOfLetters); // { =1, a=3, r=1, t=1, e=1, l=1, m=2}
int numberOfM = countOfLetters.get('m'); // 2