给定的英文片段:
If someone loves a flower, of which just one single blossom grows in all the millions and millions of stars, it is enough to make him happy just to look at the stars. He can say to himself, "Somewhere, my flower is there…" But if the sheep eats the flower, in one moment all his stars will be darkened… And you think that is not important.
实现代码如下:(参考网上代码,并进行了优化,再次记录下,日后细嚼)
package mianshiti;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
public class WordCounts {
public static void main(String[] args) throws Exception {
//System.out.println("请输入你要统计的字符串:按回车确认");
int counter=0;
String str1="If someone loves a flower, "+
" of which just one single blossom grows in all the millions and millions "+
"of stars, it is enough to make him happy just to look at the stars. He can say to himself,"+
"\"Somewhere,my flower is there…\""+
" But if the sheep eats the flower,"+
"in one moment all his stars will be darkened… And you think that is not important.";
/*
String str2 = str1.replaceAll("[^(a-zA-Z)]"," ");
String str3[] = str2.split(" ");
for (int i = 0; i < str3.length; i++) {
System.out.print(str3[i].toUpperCase()+" ");
}
System.out.println("\n---------------------------");
*/
str1 = str1.toUpperCase().replaceAll("[^(a-zA-Z)]"," ");
String str3[] = str1.split("\\b");
//记录单词
Map<String , Integer> map = new HashMap<String , Integer>();
//统计各个单词出现的次数
for (String s1 : str3)
{
//将字符串俩端的空格去掉
if(s1.trim().length()>0){
s1=s1.trim();
int count = str1.split("\\b"+s1.trim()+"\\b").length-1;
map.put(s1, count);
}else{
map.put("空格", counter++);
}
}
System.out.println("\n----------单词出现的次数分别为: ----------\n");
//单词出现次数
for (String key : map.keySet()) {
System.out.println(key+"出现:"+map.get(key)+"次");
}
//把元素加入list中
List<String> list = new ArrayList<String>();
//将所有的key加入list中
list.addAll(map.keySet());
// 排序前
System.out.println("\n***********加入list后的完整输出:**********\n");
ListIterator<String> it = list.listIterator();
while(it.hasNext()){
String str6 = it.next();
System.out.print(str6+" ");
}
System.out.println("\n");
System.out.println("\n------单词出现的位置如下: ----------\n");
for (String s : list) {
System.out.println(s+"出现的位置:"+list.indexOf(s));
}
// 开始排序
System.out.println("\n按照出现次数降序排列(AESC):\n");
for (int i = 1; i < list.size(); i++) {
for (int j = 0; j < list.size() - i; j++) {
if
(map.get(list.get(j)) > map.get(list.get(j+1))) {
String tmp = list.get(j);
list.set(j, list.get(j+1));
list.set(j + 1, tmp);
}
}
}
// 排序后
System.out.println("\n排序后:\n");
for (String s : list) {
System.out.println(s+"出现次数:"+map.get(s));
}
}
}
运行结果:
----------单词出现的次数分别为: ----------
THAT出现:1次
A出现:1次
ALL出现:2次
SOMEONE出现:1次
SINGLE出现:1次
BE出现:1次
SOMEWHERE出现:1次
THERE出现:1次
ONE出现:2次
BLOSSOM出现:1次
SHEEP出现:1次
SAY出现:1次
空格出现:65次
MAKE出现:1次
DARKENED出现:1次
JUST出现:2次
HIMSELF出现:1次
THE出现:4次
THINK出现:1次
HAPPY出现:1次
WHICH出现:1次
OF出现:2次
FLOWER出现:3次
IF出现:2次
ENOUGH出现:1次
BUT出现:1次
MOMENT出现:1次
MILLIONS出现:2次
IN出现:2次
IMPORTANT出现:1次
IS出现:3次
IT出现:1次
MY出现:1次
HIM出现:1次
GROWS出现:1次
CAN出现:1次
NOT出现:1次
AT出现:1次
HIS出现:1次
LOVES出现:1次
AND出现:2次
STARS出现:3次
WILL出现:1次
TO出现:3次
EATS出现:1次
LOOK出现:1次
HE出现:1次
YOU出现:1次
***********加入list后的完整输出:**********
THAT A ALL SOMEONE SINGLE BE SOMEWHERE THERE ONE BLOSSOM SHEEP SAY 空格 MAKE DARKENED JUST HIMSELF THE
THINK HAPPY WHICH OF FLOWER IF ENOUGH BUT MOMENT MILLIONS IN IMPORTANT IS IT MY HIM GROWS CAN NOT A
T HIS LOVES AND STARS WILL TO EATS LOOK HE YOU
------单词出现的位置如下: ----------
THAT出现的位置:0
A出现的位置:1
ALL出现的位置:2
SOMEONE出现的位置:3
SINGLE出现的位置:4
BE出现的位置:5
SOMEWHERE出现的位置:6
THERE出现的位置:7
ONE出现的位置:8
BLOSSOM出现的位置:9
SHEEP出现的位置:10
SAY出现的位置:11
空格出现的位置:12
MAKE出现的位置:13
DARKENED出现的位置:14
JUST出现的位置:15
HIMSELF出现的位置:16
THE出现的位置:17
THINK出现的位置:18
HAPPY出现的位置:19
WHICH出现的位置:20
OF出现的位置:21
FLOWER出现的位置:22
IF出现的位置:23
ENOUGH出现的位置:24
BUT出现的位置:25
MOMENT出现的位置:26
MILLIONS出现的位置:27
IN出现的位置:28
IMPORTANT出现的位置:29
IS出现的位置:30
IT出现的位置:31
MY出现的位置:32
HIM出现的位置:33
GROWS出现的位置:34
CAN出现的位置:35
NOT出现的位置:36
AT出现的位置:37
HIS出现的位置:38
LOVES出现的位置:39
AND出现的位置:40
STARS出现的位置:41
WILL出现的位置:42
TO出现的位置:43
EATS出现的位置:44
LOOK出现的位置:45
HE出现的位置:46
YOU出现的位置:47
按照出现次数降序排列(AESC):
排序后:
THAT出现次数:1
A出现次数:1
SOMEONE出现次数:1
SINGLE出现次数:1
BE出现次数:1
SOMEWHERE出现次数:1
THERE出现次数:1
BLOSSOM出现次数:1
SHEEP出现次数:1
SAY出现次数:1
MAKE出现次数:1
DARKENED出现次数:1
HIMSELF出现次数:1
THINK出现次数:1
HAPPY出现次数:1
WHICH出现次数:1
ENOUGH出现次数:1
BUT出现次数:1
MOMENT出现次数:1
IMPORTANT出现次数:1
IT出现次数:1
MY出现次数:1
HIM出现次数:1
GROWS出现次数:1
CAN出现次数:1
NOT出现次数:1
AT出现次数:1
HIS出现次数:1
LOVES出现次数:1
WILL出现次数:1
EATS出现次数:1
LOOK出现次数:1
HE出现次数:1
YOU出现次数:1
ALL出现次数:2
ONE出现次数:2
JUST出现次数:2
OF出现次数:2
IF出现次数:2
MILLIONS出现次数:2
IN出现次数:2
AND出现次数:2
FLOWER出现次数:3
IS出现次数:3
STARS出现次数:3
TO出现次数:3
THE出现次数:4
空格出现次数:65