thinking in java中第11章课后第9题:
原题:使用键值对填充HashMap.打印结果,证明是按散列码排序的.
代码:如下
主程序:
public class _09_HashMapAndLinkedHashMapTest {
public static void main(String[] args) {
HashMap m = new HashMap();
for(int i = 0;i<10;i++)
m.put(new Groundhog2(i), new Object());
System.out.println("Printing Map: \n" + m);//直接打印:
Set s = m.keySet();//取出键值
System.out.println("Key Set: \n" + s);//打印键值
Iterator it = s.iterator();
while (it.hasNext()) {
Object o = it.next();
System.out.println("Key=" + o + ", hashcode="//打印键值和键值的散列码
+ o.hashCode());
}
}
}
Groundhog2代码:
public class Groundhog2 extends Groundhog{
public Groundhog2(int n){
super(n);
}
//散列函数用于计算散列码
public int hashCode(){
System.out.println("Groundhog2.hashcode()");
return number;
}
public boolean equals(Object o){
System.out.println("Groundhog2.equals()");
return (o instanceof Groundhog2) && (number == ((Groundhog2)o).number);
}
}
打印效果:
Printing Map:
{Groundhog #2=java.lang.Object@d9f9c3,
Groundhog #4=java.lang.Object@9cab16,
Groundhog #9=java.lang.Object@1a46e30,
Groundhog #8=java.lang.Object@3e25a5,
Groundhog #6=java.lang.Object@19821f,
Groundhog #1=java.lang.Object@addbf1,
Groundhog #3=java.lang.Object@42e816,
Groundhog #7=java.lang.Object@9304b1,
Groundhog #5=java.lang.Object@190d11,
Groundhog #0=java.lang.Object@a90653}
Key Set:
[Groundhog #2, Groundhog #4, Groundhog #9, Groundhog #8, Groundhog #6, Groundhog #1, Groundhog #3, Groundhog #7, Groundhog #5, Groundhog #0]
Key=Groundhog #2, hashcode=2
Key=Groundhog #4, hashcode=4
Key=Groundhog #9, hashcode=9
Key=Groundhog #8, hashcode=8
Key=Groundhog #6, hashcode=6
Key=Groundhog #1, hashcode=1
Key=Groundhog #3, hashcode=3
Key=Groundhog #7, hashcode=7
Key=Groundhog #5, hashcode=5
Key=Groundhog #0, hashcode=0
问题:乍看之下:不是按散列码排序的呀?为什么呢?请指点一下?
原题:使用键值对填充HashMap.打印结果,证明是按散列码排序的.
代码:如下
主程序:
public class _09_HashMapAndLinkedHashMapTest {
public static void main(String[] args) {
HashMap m = new HashMap();
for(int i = 0;i<10;i++)
m.put(new Groundhog2(i), new Object());
System.out.println("Printing Map: \n" + m);//直接打印:
Set s = m.keySet();//取出键值
System.out.println("Key Set: \n" + s);//打印键值
Iterator it = s.iterator();
while (it.hasNext()) {
Object o = it.next();
System.out.println("Key=" + o + ", hashcode="//打印键值和键值的散列码
+ o.hashCode());
}
}
}
Groundhog2代码:
public class Groundhog2 extends Groundhog{
public Groundhog2(int n){
super(n);
}
//散列函数用于计算散列码
public int hashCode(){
System.out.println("Groundhog2.hashcode()");
return number;
}
public boolean equals(Object o){
System.out.println("Groundhog2.equals()");
return (o instanceof Groundhog2) && (number == ((Groundhog2)o).number);
}
}
打印效果:
Printing Map:
{Groundhog #2=java.lang.Object@d9f9c3,
Groundhog #4=java.lang.Object@9cab16,
Groundhog #9=java.lang.Object@1a46e30,
Groundhog #8=java.lang.Object@3e25a5,
Groundhog #6=java.lang.Object@19821f,
Groundhog #1=java.lang.Object@addbf1,
Groundhog #3=java.lang.Object@42e816,
Groundhog #7=java.lang.Object@9304b1,
Groundhog #5=java.lang.Object@190d11,
Groundhog #0=java.lang.Object@a90653}
Key Set:
[Groundhog #2, Groundhog #4, Groundhog #9, Groundhog #8, Groundhog #6, Groundhog #1, Groundhog #3, Groundhog #7, Groundhog #5, Groundhog #0]
Key=Groundhog #2, hashcode=2
Key=Groundhog #4, hashcode=4
Key=Groundhog #9, hashcode=9
Key=Groundhog #8, hashcode=8
Key=Groundhog #6, hashcode=6
Key=Groundhog #1, hashcode=1
Key=Groundhog #3, hashcode=3
Key=Groundhog #7, hashcode=7
Key=Groundhog #5, hashcode=5
Key=Groundhog #0, hashcode=0
问题:乍看之下:不是按散列码排序的呀?为什么呢?请指点一下?