一、题目要求
给定一个字符串列表List<String> strList, 统计里面每一个字符串的出现次数。
如:{"aa", "aa", "b"}
输出:{"aa", 2}, {"b", 1}
补充完整下面的方法:
public Map<String, Integer> calStringArray(List<String> strList) {
//TODO-补充内容
}
二、参考答案
参考答案:
public Map<String, Integer> calStringArray(List<String> strList) {
Map<String, Integer> results = new HashMap<>();
if (null == strList || 0 == strList.size()) {
return results;
}
for (String str : strList) {
if (results.containsKey(str)) {
results.put(str, results.get(str) + 1);
} else {
results.put(str, 1);
}
}
return results;
}