日期:2014-05-20 浏览次数:20745 次
import java.util.HashMap; import java.util.Map; class Dog{ public String name; public Dog(String name){ this.name = name; } public boolean equals(Object o){ if((o instanceof Dog) && (((Dog)o).name == name)){ return true; }else{ return false; } } public int hashCode(){ return name.length(); } public String toString(){ return name; } } public class Test111226{ public static void main(String[] args) { Map<Object, Object> m = new HashMap<Object, Object>(); Dog d1 = new Dog("clover"); m.put(d1, "DDDDOG"); System.out.println(m.get(d1)); d1.name = "magnolia"; for(Object o: m.keySet()){ System.out.println(o.hashCode()); System.out.println(o.toString()); } System.out.println("================"); System.out.println(d1.hashCode()); System.out.println(d1.toString()); System.out.println("----------------"); System.out.println(m.get(d1)); } }