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.
PreviousIs Java pass-by-value or pass-by-reference?Next Why are strings immutable in Java, and what does that cost?
Is Java pass-by-value or pass-by-reference?Why are strings immutable in Java, and what does that cost?What is the difference between int and Integer? Where does autoboxing bite?What changed in switch, and why should a sealed switch have no default?How are overloading and overriding resolved differently?