concurrenthashmap<A, String>.
我写了这样一个concurrenthashmap,A是自己定义的类,实现了equals和hashCode()方法,但是containsKey()总是找不到这样的值。下面是代码?
class A extends Object{
public String name;
A(String n) {
name = n;
}
public boolean equals(A obj) {
if (obj.name.equals(name)){
return true;
}
else{
return false;
}
}
public int hashCode(){
int num;
num = name.hashCode();
return num;
}
}public void testmap(){
A key = null;
if (m1 == null)
   m1 = new ConcurrentHashMap<A, String>(16);
if (!m1.containsKey(new A("123"))){
m1.putIfAbsent(new A("123"), "hello");
    for (Enumeration<A>e = m1.keys(); e.hasMoreElements();) {
     key = e.nextElement();
    }
}
System.out.println(m1.containsKey(key));
if (m1.get(key) == null) {
System.out.println("null pointer.");
}
else{
System.out.println(m1.get(key).toString());
}
System.out.println(m1.putIfAbsent(key, "hello world"));
System.out.println(m1.get(new A("123")));
System.out.println(key.equals(new A("123")));
System.out.println(key.hashCode() + " " + new A("123").hashCode());
System.out.println(m1.containsKey("123"));
}