import java.util.*;
public class test0510 {
public static void main(String[] args) {
//英文文章
String s = "Life is too short to spend time with people who suck the happiness out of you.If someone wants you in their life, they’ll make room for you. You shouldn’t have to fight for a spot. Never, ever insist yourself to someone who continuously overlooks your worth. And remember, it’s not the people that stand by your side when you’re at your best, but the ones who stand beside you when you’re at your worst that are your true friends.";
//创建一个空的hashmap
Map<String ,Integer> stringHashMap = new HashMap<>();
//将,.和双空格替换为单空格
String replace1 = s.replace(",", " ");
String replace2 = replace1.replace(".", " ");
String replace3 = replace2.replace(" ", " ");
//字符串按单空格切割为数组
String[] s1 = replace3.split(" ");
//将数组放进hashmap的key,value均为0
for (String string : s1) {
stringHashMap.put(string,0);
}
//遍历数组,如果hashmap的key中出现了该单词,value就+1
for (String string : s1) {
if (stringHashMap.containsKey(string)){
stringHashMap.replace(string,stringHashMap.get(string),stringHashMap.get(string)+1);
}
}
//遍历hashmap,就得到了文章中每个单词的出现的次数
Set<String> strings = stringHashMap.keySet();
for (String string : strings) {
System.out.println(string+" == "+stringHashMap.get(string));
}
}
}
二、运行结果
"C:\Program Files\Java\jdk-22\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.2\lib\idea_rt.jar=3182:C:\Program Files\JetBrains\IntelliJ IDEA 2023.2.2\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\study\heima-leadnews\heima-leadnews-test\demo\target\classes;D:\apache-maven-3.6.1\mvn_resp\org\springframework\boot\spring-boot-starter\3.2.5\spring-boot-starter-3.2.5.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\boot\spring-boot\3.2.5\spring-boot-3.2.5.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\spring-context\6.1.6\spring-context-6.1.6.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\spring-aop\6.1.6\spring-aop-6.1.6.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\spring-beans\6.1.6\spring-beans-6.1.6.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\spring-expression\6.1.6\spring-expression-6.1.6.jar;D:\apache-maven-3.6.1\mvn_resp\io\micrometer\micrometer-observation\1.12.5\micrometer-observation-1.12.5.jar;D:\apache-maven-3.6.1\mvn_resp\io\micrometer\micrometer-commons\1.12.5\micrometer-commons-1.12.5.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\boot\spring-boot-autoconfigure\3.2.5\spring-boot-autoconfigure-3.2.5.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\boot\spring-boot-starter-logging\3.2.5\spring-boot-starter-logging-3.2.5.jar;D:\apache-maven-3.6.1\mvn_resp\ch\qos\logback\logback-classic\1.4.14\logback-classic-1.4.14.jar;D:\apache-maven-3.6.1\mvn_resp\ch\qos\logback\logback-core\1.4.14\logback-core-1.4.14.jar;D:\apache-maven-3.6.1\mvn_resp\org\apache\logging\log4j\log4j-to-slf4j\2.21.1\log4j-to-slf4j-2.21.1.jar;D:\apache-maven-3.6.1\mvn_resp\org\apache\logging\log4j\log4j-api\2.21.1\log4j-api-2.21.1.jar;D:\apache-maven-3.6.1\mvn_resp\org\slf4j\jul-to-slf4j\2.0.13\jul-to-slf4j-2.0.13.jar;D:\apache-maven-3.6.1\mvn_resp\jakarta\annotation\jakarta.annotation-api\2.1.1\jakarta.annotation-api-2.1.1.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\spring-core\6.1.6\spring-core-6.1.6.jar;D:\apache-maven-3.6.1\mvn_resp\org\springframework\spring-jcl\6.1.6\spring-jcl-6.1.6.jar;D:\apache-maven-3.6.1\mvn_resp\org\yaml\snakeyaml\2.2\snakeyaml-2.2.jar;D:\apache-maven-3.6.1\mvn_resp\org\slf4j\slf4j-api\2.0.13\slf4j-api-2.0.13.jar;D:\apache-maven-3.6.1\mvn_resp\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;D:\apache-maven-3.6.1\mvn_resp\org\projectlombok\lombok\1.18.32\lombok-1.18.32.jar;D:\apache-maven-3.6.1\mvn_resp\junit\junit\4.13.2\junit-4.13.2.jar;D:\apache-maven-3.6.1\mvn_resp\org\hamcrest\hamcrest-core\2.2\hamcrest-core-2.2.jar com.example.demo.test0510.test0510
but == 1
shouldn’t == 1
your == 5
when == 2
that == 2
Never == 1
yourself == 1
overlooks == 1
beside == 1
you == 4
in == 1
is == 1
insist == 1
friends == 1
ever == 1
at == 2
And == 1
spot == 1
Life == 1
they’ll == 1
true == 1
worst == 1
If == 1
who == 3
for == 2
their == 1
you’re == 2
best == 1
life == 1
out == 1
worth == 1
remember == 1
it’s == 1
not == 1
too == 1
happiness == 1
are == 1
spend == 1
of == 1
by == 1
have == 1
wants == 1
stand == 2
make == 1
fight == 1
a == 1
side == 1
continuously == 1
people == 2
room == 1
the == 3
with == 1
someone == 2
short == 1
ones == 1
to == 3
time == 1
suck == 1
You == 1
进程已结束,退出代码为 0