Languagemedium0-2 years

What happens if you override equals() but not hashCode()?

Hash-based collections break. HashSet and HashMap find a bucket by hashCode first and call equals only within that bucket; two equal objects with different hashes land in different buckets, so the set holds duplicates and contains returns false for something it holds.

The lesson behind it →
More on Language