《Thinking in Java》十七章_容器深入研究_练习13(Page484)

时间:2023-12-25 15:57:49

练习13:

单词计数器

 import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map; import org.cc.foo_005.AssocitiveArray.AssocitiveArrayEntry; public class Main { public static void main(String[] args) { String path="D:/test_001/practice_002/1.Harry Potter and the Sorcerer's Stone.txt"; AssocitiveArray<String,Integer> ans=wordCount(path); Iterator<AssocitiveArrayEntry<String,Integer>> iter=ans.iterator();
while(iter.hasNext()){
AssocitiveArrayEntry<String,Integer> o=iter.next();
System.out.printf("%s=%d\n",o.getKey(),o.getValue());
} } public static AssocitiveArray<String,Integer> wordCount(String filePath){ BufferedReader reader=null;
AssocitiveArray<String,Integer> res=null; try {
reader=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8"));
StringBuilder sb=new StringBuilder(); while(reader.ready()){
sb.append(reader.readLine());
} String words[]=sb.toString().split("[(\\s{1,})]"); res=new AssocitiveArray<String,Integer>(); for(String s:words){
if(!"".equals(s)){
Integer t=res.get(s);
res.put(s,t==null?1:t+1);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(reader!=null) reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} return res;
} } //关联数组
class AssocitiveArray<K, V> implements Iterable { //存储数据
private Object pairs[][];
//当前最大有效数据的下标+1,即下一个可用位置,类似于栈顶指针
private int index; public AssocitiveArray() {
this(17);
} public AssocitiveArray(int length) {
pairs=new Object[length][2];
} public void put(K key,V value){
//先检查,如果key已经存在的话就直接覆盖掉
for(int i=0;i<index;i++){
if(key.equals(pairs[i][0])){
pairs[i][1]=value;
return ;
}
}
//检查是否需要扩充空间
if(index==pairs.length) pairs=Arrays.copyOf(pairs,pairs.length*2);
pairs[index++]=new Object[]{key,value};
} public V get(K key){
for(int i=0;i<index;i++){
if(key.equals(pairs[i][0])) return (V) pairs[i][1];
}
return null;
} public V remove(K key){
for(int i=0;i<index;i++){
if(key.equals(pairs[i][0])){
V oldValue=(V) pairs[i][1];
pairs[i]=pairs[--index];
return oldValue;
}
}
return null;
} public int size(){
return index;
} @Override
public String toString() {
StringBuilder sb=new StringBuilder();
sb.append("[");
for(int i=0;i<index;i++){
sb.append(pairs[i][0]).append(":").append(pairs[i][1].toString());
if(i!=pairs.length-1) sb.append(",");
}
sb.append("]");
return sb.toString();
} @Override
public Iterator<AssocitiveArrayEntry<K,V>> iterator() {
return new Iterator<AssocitiveArrayEntry<K,V>>() { private int curIndex; @Override
public boolean hasNext() {
return curIndex<index;
} @Override
public AssocitiveArrayEntry<K,V> next() {
AssocitiveArrayEntry<K,V> ans=new AssocitiveArrayEntry<K,V>((K)pairs[curIndex][0],(V)pairs[curIndex][1]);
curIndex++;
return ans;
} @Override
public void remove() {
throw new UnsupportedOperationException();
}
};
} static class AssocitiveArrayEntry<K,V> implements Map.Entry<K,V>{ private K key;
private V value; public AssocitiveArrayEntry(K key, V value) {
super();
this.key = key;
this.value = value;
} @Override
public K getKey() {
return key;
} @Override
public V getValue() {
return value;
} @Override
public V setValue(Object value) {
throw new UnsupportedOperationException();
} } }

测试结果如图:

《Thinking in Java》十七章_容器深入研究_练习13(Page484)