练习12:
练习使用HashMap / LinkedHashMap / TreeMap
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap; public class Main { public static void main(String[] args) { /*--------------- HashMap -------------------------*/ Map<String,String> map1=new HashMap<String,String>(); map1.put("sky","blue");
map1.put("grass","green");
map1.put("ocean","dancing");
map1.put("tree","tall");
map1.put("earth","brown");
map1.put("sun","warm");
map1.put("extra","object"); log(map1);
log(map1.get("ocean")); /*--------------- TreeMap -------------------------*/ Map<String,String> map2=new TreeMap<String,String>(); map2.put("sky","blue");
map2.put("grass","green");
map2.put("ocean","dancing");
map2.put("tree","tall");
map2.put("earth","brown");
map2.put("sun","warm");
map2.put("extra","object"); log(map2);
log(map2.get("ocean")); /*--------------- LinkedHashMap -------------------------*/ Map<String,String> map3=new LinkedHashMap<String,String>(); map3.put("sky","blue");
map3.put("grass","green");
map3.put("ocean","dancing");
map3.put("tree","tall");
map3.put("earth","brown");
map3.put("sun","warm");
map3.put("extra","object"); log(map3);
log(map3.get("ocean")); } public static void log(Object ...args){
for(int i=0;i<args.length;i++){
System.out.print(args[i]+" ");
}
System.out.println();
} }
单词统计的一般过程:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA44AAAA7CAIAAAC7ekOYAAAgAElEQVR4nO2d7VMb1/XH9cfsXyHe7CteUEZKmKFmMhOQEpjYDBNWtsNA4zRI47YwmfBgaIe6BTGOPa4taI3jhOexU4oA/xwcMKS1wYAyjgRIohgBkpxg/V6ccHp97+5q9YBWK+7nBSOtdu/eveeec773QcKU4HA4HE6WqK6uFjlyVFdX620cDodjSEzZLe7x48eRSCS7ZeYzPp9vZWVF71roAM/HnFxiIJUjiuJrjhyiKOptHA4nfQKBwOlM99qJx+MnJAIzkqpzc3NtbW2SJFVWVkJGkSSptLRUz5yWW0pLS1G01dbWSpLkdrtPQ28WeT7m5BDROCqHu4YSoij6/f5gMKi3iZLDh+JKGGjQmCGBQGBoaKihoUGSpOLiYlEUKyoqTpW8SYP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H5XV9fg4ODExAS5qgxsbm6Gw+E0vlOeplQFXr9+HY1Gd3d3Nzc3qQoFg8FcNU4+EgqFIpHITz/9lEnz5j96NzPn1KF3l08OH8UpYaDxBsDmNQ6wubmpt3FywU8//bS/vx8KhajHP+XyJlUCgcDm5ubu7u7h4WHaa8sZSVWKeDwej8f39/f39vY07j8oDOCRo9FoPB4veHlKwkM5J5cYJUHq3U75jt724XBS5ueff47H47FYbG9vT8vXgE4zr169glYCTZiV7+dkU6pyOBwOhw/hVDDKeIPD4eQP/w+P0w9l8PTsHAAAAABJRU5ErkJggg==" alt="" />
使用HashMap的实现如下:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map; public class Main { public static void main(String[] args) { String path="E:/英语/阅读训练/哈利波特英文版1-7全集/1.Harry Potter and the Sorcerer's Stone.txt"; Map<String,Integer> map=wordCount(path); for(String s:map.keySet()){
System.out.printf("%s=%d\n",s,map.get(s));
} } public static Map<String,Integer> wordCount(String filePath){ BufferedReader reader=null;
Map<String,Integer> map=new HashMap<String,Integer>(); try {
reader=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8")); StringBuilder sb=new StringBuilder(); String s=reader.readLine();
while(s!=null){
sb.append(s);
s=reader.readLine();
} //先进行预处理,然后再分割
String words[]=preprocess(sb.toString()).split("\\s{1,}"); //统计词频
for(String e:words){
if("".equals(s)) continue;
Integer t=map.get(e);
map.put(e,t==null?1:t+1);
} } catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(reader!=null) reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} return map;
} //对内容进行预处理,去除一些干扰元素之类的
public static String preprocess(String s){
return s.replaceAll("\\s{1,}"," ").replaceAll("[,.'\"?!-;*]"," ").toLowerCase();
} }