← Quiz: Immutability and Value Objects: all 10 questions
Quiz: Immutability and Value Objects · Question 4 of 10
Why is it important to override hashCode whenever you override equals?
Why is it important to override hashCode whenever you override equals?
- A.The compiler refuses to compile otherwise
- B.Equal objects must have equal hash codes, or HashSet and HashMap will look in the wrong bucket
- C.hashCode makes equals run faster
- D.It is only needed for records
Answer & explanation
Correct answer: Equal objects must have equal hash codes, or HashSet and HashMap will look in the wrong bucket
Hash-based collections use hashCode first, then equals.
Can you get the rest right without peeking?
Take the timed Quiz: Immutability and Value Objects quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →