哪位大牛解释一下这个#1
--------------------output--------
Insert
hashCode(clover)
hashCode(clover2)
equals(clover2, clover)
Search
hashCode(aiko) // #1 why is it used in find("k1")?
Dog@4
hashCode(clover)
equals(clover, clover2)
Dog key
----------code
import java.util.ArrayList;
import java.util.List;
import java.util.*;
class Dog {
public Dog(String n) {
name = n;
}
public String name;
public boolean equals(Object o) {
System.out.printf("equals(%s, %s)n", name, ((Dog) o).name);
if ((o instanceof Dog) && (((Dog) o).name == name)) {
return true;
} else {
return false;
}
}
public int hashCode() {
System.out.printf("hashCode(%s)n", name);
//return name.length();
return 4;
}
}
public class Main {
public static void main(String[] args) {
Map
i**p 发帖数: 902
2
哪位大牛解释一下这个#1
--------------------output--------
Insert
hashCode(clover)
hashCode(clover2)
equals(clover2, clover)
Search
hashCode(aiko) // #1 why is it used in find("k1")?
Dog@4
hashCode(clover)
equals(clover, clover2)
Dog key
----------code
import java.util.ArrayList;
import java.util.List;
import java.util.*;
class Dog {
public Dog(String n) {
name = n;
}
public String name;
public boolean equals(Object o) {
System.out.printf("equals(%s, %s)n", name, ((Dog) o).name);
if ((o instanceof Dog) && (((Dog) o).name == name)) {
return true;
} else {
return false;
}
}
public int hashCode() {
System.out.printf("hashCode(%s)n", name);
//return name.length();
return 4;
}
}
public class Main {
public static void main(String[] args) {
Map m = new HashMap();
System.out.println("Insert");
m.put("k1", new Dog("aiko")); // add some key/value pairs
Dog d1 = new Dog("clover"); // let's keep this reference
m.put(d1, "Dog key");
Dog d2 = new Dog("clover2");
m.put(d2, "Dog key 2");