在学习think in java容器深入研究--填充容器这一章节时,刚开始看到的例子很是迷惑,后来做了下仔细研究,现在分享下。
摘自think in java中的一段代码:
//: net/mindview/util/CountingMapData.java
// Unlimited-length Map containing sample data.
package net.mindview.util;
import java.util.*;
import java.util.Map.Entry;
public class CountingMapData extends AbstractMap<Integer, String> {
private int size;
private static String[] chars = "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"
.split(" ");
public CountingMapData(int size) {
if (size < 0)
this.size = 0;
this.size = size;
}
private static class Entry implements Map.Entry<Integer, String> {
int index;
Entry(int index) {
this.index = index;
}
public boolean equals(Object o) {
return Integer.valueOf(index).equals(o);
}
public Integer getKey() {
return index;
}
public String getValue() {
return chars[index % chars.length]
+ Integer.toString(index / chars.length);
}
public String setValue(String value) {
throw new UnsupportedOperationException();
}
public int hashCode() {
return Integer.valueOf(index).hashCode();
}
}
public Set<Map.Entry<Integer, String>> entrySet() {
// LinkedHashSet retains initialization order:
Set<Map.Entry<Integer, String>> entries = new LinkedHashSet<Map.Entry<Integer, String>>();
for (int i = 0; i < size; i++)
entries.add(new Entry(i));
return entries;
}
public static void main(String[] args) {
System.out.println(new CountingMapData(60));
}
} /*
* Output: {0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0, 9=J0,
* 10=K0, 11=L0, 12=M0, 13=N0, 14=O0, 15=P0, 16=Q0, 17=R0, 18=S0, 19=T0,
* 20=U0, 21=V0, 22=W0, 23=X0, 24=Y0, 25=Z0, 26=A1, 27=B1, 28=C1, 29=D1,
* 30=E1, 31=F1, 32=G1, 33=H1, 34=I1, 35=J1, 36=K1, 37=L1, 38=M1, 39=N1,
* 40=O1, 41=P1, 42=Q1, 43=R1, 44=S1, 45=T1, 46=U1, 47=V1, 48=W1, 49=X1,
* 50=Y1, 51=Z1, 52=A2, 53=B2, 54=C2, 55=D2, 56=E2, 57=F2, 58=G2, 59=H2}
*/// :~
开始我很疑惑为什么会输出这么些数值对这个低级问题,并且感觉有些方法看着好像很多余。
后来才发现在它继承的AbstractMap类中有一个toString()方法,通过此方法输出的结果。
public String toString() {
StringBuffer buf = new StringBuffer();
buf.append("{");
Iterator i = entrySet().iterator();
boolean hasNext = i.hasNext();
while (hasNext) {
Entry e = (Entry) (i.next());
Object key = e.getKey();
Object value = e.getValue();
buf.append((key == this ? "(this Map)" : key) + "="
+ (value == this ? "(this Map)" : value));
hasNext = i.hasNext();
if (hasNext)
buf.append(", ");
}
buf.append("}");
return buf.toString();
}
在toString方法中可以看到entrySet().iterator();和Entry e = (Entry) (i.next());两段代码,
在AbstractMap类中entrySet().为抽象方法。
在AbstractMap类中Entry为一个接口,接口中定义getKey()、getValue()、setValue(Object value)、equals(Object o)、hashCode()这几个方法。
所以在上面例子中需要我们去实现抽象接口及抽象类的方法。
经过这么一番折腾代码看着就顺畅了 哈哈 ^_^