使用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);
}
}
原文地址:https://www.cnblogs.com/aboutblank/p/2414414.html