就这样吧……明天写个能排序的(逃
呜呜呜我Java好菜
package hhh;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
public class Wordcount {
public static void main(String[] args) {
String line;
HashMap <String, Integer> map = new HashMap<String , Integer>();
try(BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("D:/temp1/abc.txt")))){
while((line = in.readLine()) != null) {
String []word = line.split(" ");
for(String str : word) {
if(str != null) {
if(map.containsKey(str)) {
map.put(str, map.get(str)+1);
}else {
map.put(str, 1);
}
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
traverseByKeySet(map);
}
private static void traverseByKeySet(HashMap<String, Integer> map) {
String key;
Integer value;
Iterator<String> iter = map.keySet().iterator();
while(iter.hasNext()) {
key = iter.next();//获取key
value = map.get(key);//获取value
System.out.println("key:"+key+" value:"+value);
}
}
}