任务描述
本关任务:编写一个能计算一段文本内容中出现单词的次数的降序排列的小程序。
相关知识
为了完成本关任务,你需要掌握:
1.如何统计相同单词的次数;
2.如何进行排序。
统计相同单词的次数
//使用map集合进行存储
String s="Day by Day";
Map<String,Integer> map=new HashMap<String,Integer>();
StringTokenizer tokenizer=new StringTokenizer(s);
int count;//记录次数
String word;//单个单词
while(tokenizer.hasMoreTokens()){
word=tokenizer.nextToken(" ");
if(map.containsKey(word)){
//拿到之前存在map集合中该单词的次数
count=map.get(word);
map.put(word, count+1);
}else{
map.put(word, 1);
}
}
Set<Entry<String, Integer>> entrySet = map.entrySet();
for (Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey()+"-"+entry.getValue());
}
输出: by-1
Day-2
如何进行排序
使用Collections包装类。它包含有各种有关集合操作的静态多态方法。
//可根据指定比较器产生的顺序对指定列表进行排序。
Collections.sort(List<T> list, Comparator<? super T> c)
示例如下:
//以上实例中的map集合为例 将map集合的每一项添加进list集合中
List<Map.Entry<String, Integer>> infos = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(infos, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
//前者-后者 升序 后者-前者 降序
return (o2.getValue() - o1.getValue());
}
});
输出:Day-2
by-1
编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End
区域内进行代码补充,具体任务如下:
- 将指定文本(可以通过右侧文件目录下的
src/step3/readme.txt
查看)以降序的方式输出每个单词出现的次数。
测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。
预期输出: 参考右边测试集中的输出。
开始你的任务吧,祝你成功!
代码示例
StudentDemo.java
package step3;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
public class StudentDemo {
// 获取单词的数量
public Map<String, Integer> getWordCount(String str) {
Map<String, Integer> map = new HashMap<String, Integer>();
//请在此添加实现代码
/********** Begin **********/
StringTokenizer stn = new StringTokenizer(str, " ;’,?.!:\n");
while (stn.hasMoreTokens()) {
String str1 = stn.nextToken();
if (map.containsKey(str1)) {
map.put(str1, map.get(str1) + 1);
} else {
map.put(str1, 1);
}
}
/********** End **********/
return map;
}
}
test.java
package step3;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
public class test{
public static void main(String[] args) throws IOException {
StudentDemo demo=new StudentDemo();
BufferedReader buffer=new BufferedReader(new FileReader("src/step3/readme.txt"));
String str="",s="";
while((str=buffer.readLine())!=null){
s+=str;
}
Map<String, Integer> wordCount = demo.getWordCount(s);
List<Entry<String, Integer>> sortWordCount = sortWordCount(wordCount);
for (Entry<String, Integer> entry : sortWordCount) {
System.out.println(entry.getKey()+"-"+entry.getValue());
}
}
//对单词出现的次数进行排序
public static List<Entry<String, Integer>> sortWordCount(Map<String, Integer> map) {
List<Map.Entry<String, Integer>> infos = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(infos, new Comparator<Map.Entry<String,Integer>>() {
@Override
public int compare(Entry<String, Integer> o1,
Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
});
return infos;
}
}
readme.txt
There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.The happiest of people don't necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can't go on well in lifeuntil you let go of your past failures and heartaches.