字元串"wo ai java abc java abc wo",統計字元串中每個單詞出現的次數
package work;
import java.awt.RenderingHints.Key;
import java.util.HashMap;
/*
* 3.字元串"wo ai java abc java abc wo",統計字元串中每個單詞出現的次數
*/
public class Work03_StringCount {
public static void main(String[] args) {
String str= "wo ai java abc java abc wo";
String[] newStr = str.split(" ");
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
for(int i = 0 ; i < newStr.length; i++) {
String key = newStr[i];
Integer value = hashMap.get(key);
if(value == null) {
hashMap.put(key, 1);
}else {
value++;
hashMap.put(key, value);
}
}
System.out.println(hashMap);
}
}