Using intermediate array for hashCode and equals
- by Basilevs
As its a pain to handle structural changes of the class in two places I often do:
class A {
private B bChild;
private C cChild;
private Object[] structure() {
return new Object[]{bChild, cChild};
}
int hashCode() {
Arrays.hashCode(structure);
}
boolean equals(Object that) {
return Arrays.equals(this.structure(), ((A)that).structure());
}
}
What's bad about this approach besides boxing of primitives?
Can it be improved?