/*1.统计每个单词出现的次数
2.有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
*/
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class Deom04 {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
String s= "If you want to change your fate I think you must come to the dark horse to learn java";
String[] data = s.split(" ");
for (String aChar : data) {
if(map.get(aChar)==null){
map.put(aChar,1);
}else {
map.put(aChar,map.get(aChar)+1);
}
}
Set<String> keySet = map.keySet();
for (String key : keySet) {
Integer value = map.get(key);
System.out.println(key+"="+value);
}
}
}
统计每个单词出现的次数
最新推荐文章于 2023-09-19 14:40:02 发布