Skip to content
Sharp

Collections · interview question

How does a HashSet ensure all elements are unique when storing objects?

Multiple choiceHardchapter-4-curatedpremiumcollectionsset-interface-and-hashset
Choose one answer
Answer and explanation

Correct answer

A. By using the hashCode() and equals() methods

Why

HashSet uses the hashCode() and equals() methods to ensure uniqueness. The hashCode() determines the bucket, and equals() checks for equality within the bucket.

Test yourself on Collections

A free test with instant, explained results. No account needed.

Start free test
All Collections questions →