Using hashcode for a unique ID

前端 未结 4 808
栀梦
栀梦 2020-12-17 20:31

I am working in a java-based system where I need to set an id for certain elements in the visual display. One category of elements is Strings, so I decided to use the String

4条回答
  •  被撕碎了的回忆
    2020-12-17 21:09

    Hashes are not unique, hence they are not apropriate for uniqueId.

    As to probability of hash collision, you could read about birthday paradox. Actually (from what I recall) when drawing from an uniform distribution of N values, you should expect collision after drawing $\sqrt(N)$ (you could get collision much earlier). The problem is that Java's implementation of hashCode (and especially when hashing short strings) doesnt provide uniform distribution, so you'll get collision much earlier.

提交回复
热议问题