I always thought hat HashMap is identifying its keys by using hasCode and equals. But this code still does not give me the value back. What am I missing?
import java.util.HashMap;
import java.util.Map;
/**
* Created by kic on 25.04.15.
*/
public class TypePair {
private final Class a;
private final Class b;
public TypePair(Class a, Class b) {
this.a = a;
this.b = b;
}
public TypePair(Object a, Object b) {
this.a = a.getClass();
this.b = b.getClass();
}
public boolean isPair (Object a, Object b) {
return this.a.isAssignableFrom(a.getClass()) && this.b.isAssignableFrom(b.getClass());
}
@Override
public boolean equals(Object obj) {
if (obj instanceof TypePair) {
return a.isAssignableFrom(((TypePair) obj).a) && b.isAssignableFrom(((TypePair) obj).b);
} else {
return false;
}
}
@Override
public int hashCode() {
return 1;
}
public static void main(String[] args) {
TypePair a = new TypePair(Number.class, String.class);
TypePair b = new TypePair(12, "hello");
Map<TypePair, Boolean> test = new HashMap<>();
test.put(a, true);
System.out.println(a.hashCode() == b.hashCode());
System.out.println(a.equals(b));
System.out.println("und? " + test.get(a));
System.out.println("und? " + test.get(b));
}
}
Prints
true
true
und? true
und? null
Aucun commentaire:
Enregistrer un commentaire