java 计算i 出现的次数_JAVA算法:按照给定的段落统计单词出现次数(JAVA代码)...
https://blog.csdn.net/seagal890/article/details/92067644
JAVA算法:按照給定的段落統(tǒng)計(jì)單詞出現(xiàn)次數(shù)(JAVA代碼)
寫一個(gè) JAVA程序以統(tǒng)計(jì)一個(gè)文本文件?words.txt?中每個(gè)單詞出現(xiàn)的頻率。
為了簡單起見,你可以假設(shè):
words.txt只包括小寫字母和?' '?。
每個(gè)單詞只由小寫字母組成。
單詞間由一個(gè)或多個(gè)空格字符分隔。
示例:
假設(shè) words.txt 內(nèi)容如下:
the day is sunny the the
the sunny is is
你的腳本應(yīng)當(dāng)輸出(以詞頻降序排列):
the 4
is 3
sunny 2
day 1
說明:
不要擔(dān)心詞頻相同的單詞的排序問題,每個(gè)單詞出現(xiàn)的頻率都是唯一的。
算法設(shè)計(jì)
package com.bean.algorithm.basic;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
public class CountWords {
public static void main(String[] args) {
long startTime = System.currentTimeMillis(); // 獲取開始時(shí)間
String string = "";
Map map = new HashMap();
try {
//讀取文件
FileInputStream fis = new FileInputStream("G://CountWords.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String temp = "";
try {
while ((temp = br.readLine()) != null) {
string = string + temp;
}
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
// 分割字符串
StringTokenizer st = new StringTokenizer(string); // 用于切分字符串
//初始化計(jì)數(shù)器
int count;
//初始化word變量
String word;
while (st.hasMoreTokens()) {
//逗號(hào),問號(hào),句號(hào),感嘆號(hào),冒號(hào),雙引號(hào),單引號(hào),換行符號(hào)
word = st.nextToken(",?.!:\"\"' '\n");
if (map.containsKey(word)) {
// HashMap 保存數(shù)據(jù)
count = map.get(word);
//計(jì)數(shù)器累加
map.put(word, count + 1);
} else {
map.put(word, 1);
}
}
// 排序
Comparator> valueComparator = new Comparator>() {
public int compare(Map.Entry o1, Map.Entry o2) {
return o2.getValue() - o1.getValue();
}
};
// 輸出結(jié)果
List> list = new ArrayList>(map.entrySet());
Collections.sort(list, valueComparator);
System.out.println("---------------------Words分析結(jié)果 ——— 輸出結(jié)果----------");
for (Map.Entry entry : list) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
long endTime = System.currentTimeMillis(); // 獲取結(jié)束時(shí)間
System.out.println("程序運(yùn)行時(shí)間: " + (endTime - startTime) + "ms");
}
}
樣例文本如下:
if you just want to try running findbugs against your own code, you can run findbugs using javawebstart. this will use our new gui under Java 1.5+ and our old gui under java 1.4. the new gui provides a number of new features, but requires java 1.5+. both use exactly the same analysis engine.
程序運(yùn)行結(jié)果
---------------------Words分析結(jié)果 ——— 輸出結(jié)果----------
new:3
1:3
gui:3
use:2
our:2
java:2
5+:2
you:2
the:2
findbugs:2
under:2
but:1
code:1
against:1
own:1
run:1
your:1
running:1
can:1
number:1
features:1
same:1
engine:1
and:1
provides:1
of:1
if:1
just:1
Java:1
a:1
using:1
will:1
old:1
want:1
this:1
exactly:1
analysis:1
both:1
4:1
javawebstart:1
try:1
to:1
requires:1
程序運(yùn)行時(shí)間: 6ms
標(biāo)簽:段落,util,JAVA,java,word,單詞,map,new,import
來源: https://blog.csdn.net/mrlin6688/article/details/100556510
總結(jié)
以上是生活随笔為你收集整理的java 计算i 出现的次数_JAVA算法:按照给定的段落统计单词出现次数(JAVA代码)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎样养成易瘦体质
- 下一篇: java计算字符串中字符出现的次数_ja