Forums Register Login

incorrect implementation of hashCode

+Pie Number of slices to send: Send
Can you show me an example of incorrect implementation of hashCode? I mean implementation which causes, that JVM is not able to locate the object in given collection.
+Pie Number of slices to send: Send
Now I found one...implementation using transient attribute. Does anybody know any other?
+Pie Number of slices to send: Send
This could also be one more incorrect implementation of hashCode().

hashCode() returning jst constant value like below is also not a gud implementation.



returning hashCode() value as 1 is not a very gud design becoz in this case, all the objects will land in the same bucket and in that case whole searching operation must be performed by .equals(Object) method.
In Short, Map is not efficient in searching.

Implement hashCode() in such a way that all objects land in different buckets would be a gud design in terms of efficiency of the Map.

Regards

Naseem Khan
+Pie Number of slices to send: Send
Constant is inefficient but correct.
+Pie Number of slices to send: Send
It's hard to define one that's incorrect in isolation because its correctness is tied up with the definition of equals() in the same class. This one comes pretty close to being utterly incorrect, though, in the sense that the value returned is utterly useless:

+Pie Number of slices to send: Send
Yes, that's nice example. I wanted to know some incorrect examples, because I couldn't imagine some.

Another incorrect could be based on current time/date - but that's pretty weird idea :-)
Just the other day, I was thinking ... about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 890 times.
Similar Threads
Please Explain (Hashcode)
Doubt in hashCode()
equals and hashcode..
Can anyone give me an example ?
HashSet not using overridden equals method
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 05:42:16.