下面的代码是计算字符。我对hashmaps有点新意,所以我对语法以及它从这一行做了什么感到困惑:Map charCounter = new TreeMap();有人可以请这段代码愚蠢,请从那条线解释一下吗?
public class repeated {
public static void main(String[] args) {
String str = "Abaa";
char[] char_array = str.toCharArray();
System.out.println("The Given String is : " + str);
Map<Character, Integer> charCounter = new TreeMap<Character, Integer>();
for (char x : char_array) {
charCounter.put(x,charCounter.get(x) == null ? 1 : charCounter.get(x) + 1);
}
for (Character key : charCounter.keySet()) {
System.out.println("occurrence of '" + key + "' is "+ charCounter.get(key));
}
}
}