equals() without hashCode() in a HashSet
QuizMedium-HardJava onlyAsked in: Cognitree, Deloitte
A User class overrides equals() to compare emails but does not override hashCode(). The hashCode() method gives an object a number that a HashSet uses to choose a bucket (a slot) for it. Two User objects with the same email are added to a HashSet. What is the most likely result?
- The set holds both objects
- The set holds one object, because equals() returns true
- It does not compile
- A runtime exception is thrown
This question is part of Pro+
Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.