题目:
正则表达式参考: https://blog.csdn.net/smallhc/article/details/80511010
package com.xu.test;
import java.util.HashMap;
public class Test4 {
// 统计字符串串中的每个单词个数,这里的单词指的是连续的不是空格的字符。请注意,你可以假定字符
// 串里里不不包括任何不不可打印的字符。
public static void main(String[] args) {
String str = "Hello python. Hello world";
String s1 = str.replaceAll("\\p{Punct}","");
String[] s = s1.split(" ");
HashMap<String,Integer> map = new HashMap<>();
for(int i=0 ; i<s.length; i++){
if(map.containsKey(s[i])){ //如果hashmap里已经有了key则在此基础上+1
map.put(s[i],map.get(s[i])+1);
}
else{
map.put(s[i],1); //如果hashmap里没有key则是第一次出现,初始化为1
}
}
System.out.println(map.toString());
// map.forEach((e1,e2)->{
// System.out.println(e1 +":"+ e2 );
// });
}
}