使用Hashmap统计String出现次数

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
/**
* Created by IntelliJ IDEA.
* User: mayday
* Date: 12-3-23
* Time: 下午8:39
*/
public class StringCount {
public static void main(String[] args) {
String t = "";
int count;
HashMap<String, Integer> strcount = new HashMap<String, Integer>();
try {
BufferedReader fs = new BufferedReader(new FileReader("read.txt"));
do {
t = fs.readLine();
if (t == null) break;
String[] str = t.split(" ");
for (String st : str) {
if (strcount.containsKey(st)) {
count = strcount.get(st);
strcount.put(st, ++count);
} else {
strcount.put(st, 1);
}
}
}
while (t != null);
} catch (IOException e) {
System.out.println(e.getMessage());
}
System.out.println(strcount);
}
}
posted @ 2012-03-23 23:12  丶Blank  阅读(428)  评论(0编辑  收藏  举报