Question

I am trying to create a zookeeper node with unix path as values(like /x/home/rrs/data0) , but this is not allowed.

So I thought of generating a hash code of path and then use it to create a node. But I read following about the hashcode:

  • Hash should not be used in a distributed application.
  • There might be collisions, For example, the Strings "Aa" and "BB" produce the same hashCode: 2112

Should i go ahead with hash code or What other options i have for my use case?

Also if i keep the string same all the time , is it guaranteed to generate same hashCode every time?

Was it helpful?

Solution

Yes, the same string will always generate the same hash-code.

Hash-codes do collide, the chance that similar (but different) strings will collide is just very very small (that's the generic idea). Your application should be able to recover (at least not break) from a collision.

What are the nature of the strings? Are they only letters? Maximum length? These properties can be used to generate a better hashcode. One of the nicest techniques I know of is Zobrist keys. Depending on the nature of your strings, this may be an option.

OTHER TIPS

This depends on what you are trying to do.

But you're right: Java hashCodes are not designed to be collision free.

If you need some kind of unique identifier you could use a cryptographic hash function (like e.g. SHA-256, MD5 etc.) over your string.

If you just have a problem with some characters in your string just replace them e.g. with underscore.

Depending on what Zookeeper is/does perhaps hashCode is not a problem at all. EHCache uses it and it's perfectly fine there for distibuted hash tables.

It's a pitty, but hashCode of String really does generate the same hash code all the time for the same string. This is because it is documented and therefore cannot be changed. (But note: this doesn't include different representations of the same string like it's possible in unicode. But I think this is not a problem here.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top