以前经常在CSDN博客上看别人的文章,有时是遇到难题为了寻找思路,有时则是为了偷懒,直接复制别人的源代码,所以对于写这些博客的技术大神,我都是佩服且感激的。感激于他们把我的问题分析的如此透彻明白,佩服于他们遇到问题不但自己能解决,还把思路分享给我,使我也能够受益。今天,这些‘雷锋“或者”红领巾“传递的正能量终于感动了我(实际上是teacher的作业...哎),我也要写自己的技术博客啦,好吧,废话不多说,开始写思路!
统计字符串在某一文本出现的概率,思路上比较简单,分为以下几步:
1.读取项目的文件位置,将读出的字节流转化为字符串;
2.字符串切分,利用空格符把独处的字符串切分为字符串数组;
3.字符串统计,遍历生成字符串数组,将这些字符串数组存储为hanshMap形式,key为字符串,value为出现次数;
4.对hanshMap的value排序,即可得到相应结果。
OK ,就是这么简单,剩下的可能就是有些具体代码大家会遇到点问题,所以直接贴代码:
<textarea readonly="readonly" name="code" class="java">
package com.swhk.main;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
/**
* Author:zhangjw
* Date:2014/9/28 16:37
* function:统计一段字符串中出现频率最高的前10个词
*
* */
public class Count {
public static void main(String[] args) {
Count c = new Count();
String filepath = new File("File").getAbsolutePath()+"\\in.txt";
String source = c.getStringFrom(filepath);
String[] stringArray = source.split(" ");
HashMap<String , Integer> map = new HashMap<String , Integer>();
map.put(stringArray[0], 0);
for(int i = 0; i < stringArray.length; i++)
{
Iterator iterator = map.keySet().iterator();
int flag = 0;
while (iterator.hasNext()) {
Object key = iterator.next();
if(map.containsKey(stringArray[i]))
{
int value = map.get(stringArray[i]);
value++;
map.put(stringArray[i], value);
flag = 1;
break;
}
}
if(flag == 0)
{
map.put(stringArray[i], 1);
}
}
map = (HashMap<String, Integer>)new Count().sortByValue(map);
Iterator iterator = map.keySet().iterator();
int count = 0;
System.out.println("频率最高的10个词语为:");
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println(key.toString()+" "+map.get(key)+"次");
if(count++ == 10)
{
break;
}
}
}
public Map sortByValue(Map<String, Integer> map) {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator(){
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry)o2).getValue())
.compareTo(((Map.Entry)o1).getValue());
}
});
Map result = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map .Entry) it.next();
result.put(entry.getKey(), entry.getValue());
}
return result;
}
public String getStringFrom(String filepath) {
File file = new File(filepath);
StringBuilder sb = new StringBuilder();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(file));
String temp = "";
try {
while ((temp = br.readLine())!=null) {
sb.append(temp+" ");
}
br.close();
String result = sb.toString();
return result;
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
最后结果如下:
使用 VisualVM进行性能检测,结果如下:
附文本内容:
<textarea readonly="readonly" name="code" class="c++">My father was a self-taught mandolin player. He was one of the best string instrument players in our town. He could not
read music, but if he heard a tune a few times, he could play it. When he was younger, he was a member of a small country
music band. They would play at local dances and on a few occasions would play for the local radio station. He often told
us how he had auditioned and earned a position in a band that featured Patsy Cline as their lead singer. He told the family
that after he was hired he never went back. Dad was a very religious man. He stated that there was a lot of drinking and
cursing the day of his audition and he did not want to be around that type of environment.Occasionally, Dad would get out
his mandolin and play for the family. We three children: Trisha, Monte and I, George Jr., would often sing along. Songs
such as the Tennessee Waltz, Harbor Lights and around Christmas time, the well-known rendition of Silver Bells. "Silver
Bells, Silver Bells, its Christmas time in the city" would ring throughout the house. One of Dad's favorite hymns was "The
Old Rugged Cross". We learned the words to the hymn when we were very young, and would sing it with Dad when he would play
and sing. Another song that was often shared in our house was a song that accompanied the Walt Disney series: Davey Crockett.
Dad only had to hear the song twice before he learned it well enough to play it. "Davey, Davey Crockett, King of the Wild
Frontier" was a favorite song for the family. He knew we enjoyed the song and the program and would often get out the mandolin
after the program was over. I could never get over how he could play the songs so well after only hearing them a few times. I
loved to sing, but I never learned how to play the mandolin. This is something I regret to this day.
A Monkey and a Tiger
Today I am very happy to stand here, telling you a story:A Monkey and a tiger .Every one thinks the monkey is very small, but the tiger is strong.In this story Little Monkey uses his wisdow to bit the tiger. just remenber those who are weak can beat those who are strong. let's share the story.
An old tiger lives in the forest. He does not want to look for food . He often tells other animals to bring him something to eat.
One day He sees a monkey and says," I'm hungry, monkey. Go to the village and bring me a fat pig."
"Oh, Tiger," says the monkey. "I can't do that now. There is another tiger over there. He also wants a fat pig. He will not let me get anything for you to eat. I am afraid of him."
"What?" cries the tiger. "Show me that tiger. I will teach him a lesson ."
"Come with me," says the monkey.
The monkey and the tiger get to a bridge over the river. "Now look down at the water," says the monkey.
"Do you see the head, the white teeth and the big green eyes of a tiger?"
"Yes, I do," says the old tiger," I will eat him up!"
“Grr…! So you told Monkey not to bring me anything?!Grrr … I’ll kill you!”
With these words the tiger jumped into the river. He did not come out again.
“Ha,ha, ha! Goodbye, Mr Tiger,” said the monkey and he went away .
</textarea>