r/javahelp • • Mar 07 '22

HashCodes... Yes... we know... "equal Strings == Equal hashCodes!!1!".

Of course, if 2 strings are equal the hashcode will be the same,

Of course, if you use the hashCode as a persistent ID (for Database or whatever) is a bad idea, but I feel these obviously bad implementations of hashCode muddle every valid implementation the hashCode functionality may have.

What about the innate hashCode of a Java object, No one seems to succinctly address this, do the JVM even keep a registry??

If we need to "care about the hashcode" and "stop using it as an object id", then Why do we even use HashMaps or Sets, do they do something to prevent collisions? , and if they do, why isn't that handled at object instantiation...

By definition if the hashCode() method of an object is as unsafe as it is meant to be, then all those classes should also be unsafe.

Most of the claims of hashCodes being bad as an id, are so vague and allude to the same two examples, that one is left completely unconvinced about it, why would sun make something so untrustworthy?

The only... only believable claim I've seen online, is the one based on the birthday paradox which states that once there are 77 163 objects you have a 50% chance of getting 2 equal hashCodes.

Again, this is a room full of guests, what if within that room you have a bathroom??? What are the chances of getting 2 random people in that bathroom with the same birthday, I bet hashCode is extremely useful on that bathroom alone???

Does this mean that Java becomes completely useless once 77 163 items are created?

Do the JVM even keep a registry of each hashCode created so that it keeps this 50% in check and keeps the chances low??

0 Upvotes

11 comments sorted by

View all comments

Show parent comments

1

u/DelarkArms Mar 07 '22

Thank you.

What I get from your answer is that there is no reliable way of defining uniqueness without a diminishing in performance, hence the need to check for collisions on a case-by-case basis.

Your answer helped me reflect that maybe I was focusing on the wrong thing, it seems that what I need is this:

String uniqueID = UUID.randomUUID().toString();

The diminishing in performance seems to be unavoidable in this case.

2

u/[deleted] Mar 07 '22

Me again

Last night I went to sleep with your question and I'm kind of an obsessive person.

It looks that you're looking for an efficient way to check entity Identity.

For that, I would point you to some concepts of DDD.

Most of real world entities have a natural identifier, SSN for people, ISBN for books, serial numbers for hardware, etc.

You can use those as fields to check identity and as PK on database.

If you have two instances of Person with the same SSN, they ARE the same, no matter if it's named John on one and Johny on other.

Of course, I might just be overthinking all this.

1

u/DelarkArms Mar 07 '22 edited Mar 07 '22

Thanks for your reply I came across a response from mr. Jon Skeet

is just a matter of comparing the references in a bitwise fashion - checking whether or not they consist of the same bytes.

My aim was to mimic the == operator without having to cache the reference, and instead dereference the value that the JVM was using at the moment of "==" comparison.

(This was what my "two people with the same birthday in the same room, at a party with 77 163 guests" sentence was alluding to)

The misunderstanding (MY misunderstanding) was that hashCode (identityHashcode to be more precise) was being used under the hood to check for reference address check... it seemed reasonable...

By doing this I was also attempting to assign an int ID field at the moment of initialization (To prevent re-computations).

It seems accessing the inner workings of object reference comparison is not doable, so I better stick to passing the whole reference instead.

As for your advice, it is a really good one. I do manually check for Object contents, that is the only way when dealing with persistent memory.

0

u/AutoModerator Mar 07 '22

You seem to try to compare String values with == or !=.

This approach does not work reliably in Java as it does not actually compare the contents of the Strings. Since String is an object data type it should only be compared using .equals(). For case insensitive comparison, use .equalsIgnoreCase().

See Help on how to compare String values in our wiki.


Your post/comment is still visible. There is no action you need to take.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.