HashMap 统计一个字符串中每个单词出现的次数
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<>();
String str = "aaa bbb ccc aaa bbb ccc ccc bbb ccc ddd";
String[] strArr = str.split(" ");
for (String s : strArr) {
if (!map.containsKey(s)) {
map.put(s, 1);
} else {
map.put(s, map.get(s) + 1);
}
}
System.out.println(map);
}
}