统计文章的单词个数

package com.lr.string;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 WordCount {
	public static void main(String[] args) {
		String str = null;
		int WordCount = 0;
		Map<String, Integer> map = new HashMap<String, Integer>();
		List<Map.Entry<String, Integer>> listMap = null;
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File(
							"src/com/lr/string/WordCount.java"))));
			while ((str = br.readLine()) != null) {
				StringTokenizer token = new StringTokenizer(str);
				while (token.hasMoreTokens()) {
					String word = token
							.nextToken("+*!:@=\\\"\';?><,./\n\t {()");
					if (map.containsKey(word)) {
						map.put(word, map.get(word) + 1);
					} else {
						map.put(word, 1);
					}
					WordCount++;
				}
			}// while
			listMap = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
			Collections.sort(listMap,
					new Comparator<Map.Entry<String, Integer>>() {
						@Override
						public int compare(Map.Entry<String, Integer> map1,
								Map.Entry<String, Integer> map2) {
							return (map1.getValue().compareTo(map2.getValue()));
						}
					});
			for (Map.Entry<String, Integer> m : listMap) {
				System.out.println(m.getKey() + ":" + m.getValue());
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 结果:

getKey:1
for:1
args:1
package:1
println:1
else:1
static:1
get:1
compare:1
nextToken:1
class:1
compareTo:1
0:1
String[]:1
readLine:1
try:1
sort:1
entrySet:1
if:1
src:1
System:1
return:1
n:1
void:1
main:1
hasMoreTokens:1
containsKey:1
t:1
Override:1
out:1
InputStreamReader:2
com:2
br:2
Collections:2
int:2
1:2
FileNotFoundException:2
map2:2
map1:2
printStackTrace:2
put:2
IOException:2
lr:2
Comparator:2
catch:2
ArrayList:2
List:2
HashMap:2
File:2
FileInputStream:2
string:2
StringTokenizer:3
str:3
getValue:3
while:3
token:3
null:3
m:3
BufferedReader:3
public:3
listMap:4
WordCount:4
e:4
word:5
Entry:6
map:6
io:6
util:7
Map:8
new:8
Integer:8
String:10
}:12
import:13
java:14

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值