Implement hashCode and equals
Problem For a custom class (e.g. Person{name, age, email}), implement equals() and hashCode() honoring their contract.
Input / Output
- equals(Object o): logical equality over significant fields; hashCode(): int consistent with equals.
Constraints
- Contract: equal objects MUST have equal hash codes; hashCode must be consistent across calls; equals must be reflexive, symmetric, transitive, null-safe (equals(null) → false).
Example
- new Person("A", 30) and new Person("A", 30) → equals true, same hashCode → usable as HashMap keys; violating this makes map.get() miss entries.
asked …