分析以下需求,并用代码实现:
(1)统计每个单词出现的次数
(2)有如下字符串"If you want to change your fate I think you must come to the ujiuye to learn java"(用空格间隔)
(3)打印格式:
I=1
If=1
change=1
come=1
fate=1
java=1
learn=1
must=1
the=1
think=1
to=3
ujiuye=1
want=1
you=2
your=1
package text;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class text14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> list=new ArrayList<>();
Set<String> set=new TreeSet<>();
String str=sc.nextLine();
String[] arr=str.split(" ");
for(String s:arr) {
list.add(s);
}
for(String s:list) {
set.add(s);
}
for(String s:set) {
System.out.println(s+"="+Collections.frequency(list, s));
}
}
}```