《Thinking in Java》十七章_容器深入研究_练习13(Page484)
练习13:
单词计数器
1 import java.io.BufferedReader; 2 import java.io.FileInputStream; 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.util.Arrays; 7 import java.util.Iterator; 8 import java.util.Map; 9 10 import org.cc.foo_005.AssocitiveArray.AssocitiveArrayEntry; 11 12 public class Main { 13 14 public static void main(String[] args) { 15 16 String path="D:/test_001/practice_002/1.Harry Potter and the Sorcerer's Stone.txt"; 17 18 AssocitiveArray<String,Integer> ans=wordCount(path); 19 20 Iterator<AssocitiveArrayEntry<String,Integer>> iter=ans.iterator(); 21 while(iter.hasNext()){ 22 AssocitiveArrayEntry<String,Integer> o=iter.next(); 23 System.out.printf("%s=%d\n",o.getKey(),o.getValue()); 24 } 25 26 } 27 28 public static AssocitiveArray<String,Integer> wordCount(String filePath){ 29 30 BufferedReader reader=null; 31 AssocitiveArray<String,Integer> res=null; 32 33 try { 34 reader=new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8")); 35 StringBuilder sb=new StringBuilder(); 36 37 while(reader.ready()){ 38 sb.append(reader.readLine()); 39 } 40 41 String words[]=sb.toString().split("[(\\s{1,})]"); 42 43 res=new AssocitiveArray<String,Integer>(); 44 45 for(String s:words){ 46 if(!"".equals(s)){ 47 Integer t=res.get(s); 48 res.put(s,t==null?1:t+1); 49 } 50 } 51 } catch (FileNotFoundException e) { 52 e.printStackTrace(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 }finally{ 56 try { 57 if(reader!=null) reader.close(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 } 62 63 return res; 64 } 65 66 } 67 68 //关联数组 69 class AssocitiveArray<K, V> implements Iterable { 70 71 //存储数据 72 private Object pairs[][]; 73 //当前最大有效数据的下标+1,即下一个可用位置,类似于栈顶指针 74 private int index; 75 76 public AssocitiveArray() { 77 this(17); 78 } 79 80 public AssocitiveArray(int length) { 81 pairs=new Object[length][2]; 82 } 83 84 public void put(K key,V value){ 85 //先检查,如果key已经存在的话就直接覆盖掉 86 for(int i=0;i<index;i++){ 87 if(key.equals(pairs[i][0])){ 88 pairs[i][1]=value; 89 return ; 90 } 91 } 92 //检查是否需要扩充空间 93 if(index==pairs.length) pairs=Arrays.copyOf(pairs,pairs.length*2); 94 pairs[index++]=new Object[]{key,value}; 95 } 96 97 public V get(K key){ 98 for(int i=0;i<index;i++){ 99 if(key.equals(pairs[i][0])) return (V) pairs[i][1]; 100 } 101 return null; 102 } 103 104 public V remove(K key){ 105 for(int i=0;i<index;i++){ 106 if(key.equals(pairs[i][0])){ 107 V oldValue=(V) pairs[i][1]; 108 pairs[i]=pairs[--index]; 109 return oldValue; 110 } 111 } 112 return null; 113 } 114 115 public int size(){ 116 return index; 117 } 118 119 @Override 120 public String toString() { 121 StringBuilder sb=new StringBuilder(); 122 sb.append("["); 123 for(int i=0;i<index;i++){ 124 sb.append(pairs[i][0]).append(":").append(pairs[i][1].toString()); 125 if(i!=pairs.length-1) sb.append(","); 126 } 127 sb.append("]"); 128 return sb.toString(); 129 } 130 131 @Override 132 public Iterator<AssocitiveArrayEntry<K,V>> iterator() { 133 return new Iterator<AssocitiveArrayEntry<K,V>>() { 134 135 private int curIndex; 136 137 @Override 138 public boolean hasNext() { 139 return curIndex<index; 140 } 141 142 @Override 143 public AssocitiveArrayEntry<K,V> next() { 144 AssocitiveArrayEntry<K,V> ans=new AssocitiveArrayEntry<K,V>((K)pairs[curIndex][0],(V)pairs[curIndex][1]); 145 curIndex++; 146 return ans; 147 } 148 149 @Override 150 public void remove() { 151 throw new UnsupportedOperationException(); 152 } 153 }; 154 } 155 156 static class AssocitiveArrayEntry<K,V> implements Map.Entry<K,V>{ 157 158 private K key; 159 private V value; 160 161 public AssocitiveArrayEntry(K key, V value) { 162 super(); 163 this.key = key; 164 this.value = value; 165 } 166 167 @Override 168 public K getKey() { 169 return key; 170 } 171 172 @Override 173 public V getValue() { 174 return value; 175 } 176 177 @Override 178 public V setValue(Object value) { 179 throw new UnsupportedOperationException(); 180 } 181 182 } 183 184 }
测试结果如图: