Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Melody derouge
131 Pointsequals() and hashCode() overriding tips
Hi everyone
i'm learning how to work with HashSets right now , and after some research i've found out that i should override equals() and hashCode() , but i'm a bit confused because i've seen so many examples and i can't find a pattern that makes sense to me . Can someone please throughly explain to me how and why (because i don't quite understand why isn't it enough to override equals()) i should override them and if there is some tips that i should keep in mind while doing that? I would appreciate any help :)
Simon Coates
28,695 PointsSimon Coates
28,695 PointsI think hashing relates to assignment of different values into buckets (discussed at http://www.jitendrazaa.com/blog/java/what-is-the-need-to-override-hashcode-and-equals-method/ ). The hashCode method's documentation at https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html states a general contract for hashcode. Stackoverflow has a suggestion on how to write the thing at http://stackoverflow.com/questions/113511/best-implementation-for-hashcode-method .