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 trialJamie Wyton
3,011 PointsTwo objects with the same hashcode?!
How can two objects be present in the same memory location?
7 Answers
Steven Parker
231,248 PointsTwo objects cannot occupy the same memory location or index.
A hash mechanism that is not guaranteed to generate a unique value cannot be used as a "memory location" or unique key. It would be necessary to augment the mechanism to guarantee a unique value.
Otherwise, the hash code might be used as only part of the unique key, and combined with some other factors to make the final key unique.
Stephanie Hallberg
2,336 PointsSay you doing a game called TreehouseDefense and you want to override GetHashCode for Location on a Map.
Wouldn't it be easier to just return an GetHashCode method as their Locations? Example, when X is 5 and Y is 6. Why not return 56 and not include math formulas?
Another example, X is 2 and Y is 6 , then return 26?
Steven Parker
231,248 PointsThat would work as long as the maximum dimensions only required 1 digit each. But by multiplying one dimension by one more than the maximum of the other dimensions, you could ensure a unique code for any size map.
rajan lagah
2,539 Pointshash code is generated by hash function which is created such that minimum number of clashes like this occur. after this we also have technique to handle clashes .
1) eg if we have 11 elements and 11 places and place 5 is occupied and another element with hashcode 5 appear then it is shifted to 6 th position next time it will b shifted to 7.we have 11 elements 11 places so every thing will be stored we have some more techniques this is what introduction to algorithm book say .
Jamie Wyton
3,011 PointsYea I think where I was getting confused was I was thinking of the hash code as an actual memory hexadecimal location. So what is the relationship to the Equal() method then, why do we get a compilation warning of one or the other?
Steven Parker
231,248 PointsQuoting from the MSDN Documentation:
If your overridden Equals method returns true when two objects are tested for equality, your overridden GetHashCode method must return the same value for the two objects.
Jamie Wyton
3,011 PointsSo if its one object to its own Hashcode(location), then surely it would be unique anyways?
Steven Parker
231,248 PointsIt might be, depending on how the hashcode relates to the object contents.
Jamie Wyton
3,011 Pointscan you give me an example of a clash that would occur from generic hash codes, I'm still not seeing what the real point of it all is for?
Steven Parker
231,248 PointsI'm not sure what you mean by "generic", but let's imagine the hash code is generated by using the location and the formula "X + Y
". So in that case, the point at 5,2 would have the exact same code as the one at 3,4 and there would be a clash.
rajan lagah
2,539 Pointstheir is function to generating hashcode for example and just for example
function hash( String s){ //sum of ascii value of character of string }
hash(12) == ascii(1)+ascii(2) == 49+50 ==99; hash(c) == ascii(c) ==99;
hash(12)==hash(c); which is a clash
is this helpful ?