Using hashcode for a unique ID

前端 未结 4 794
栀梦
栀梦 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:07

    Hash codes can be thought of as pseudo-random numbers. Statistically, with a positive int hash code the chance of a collision between any two elements reaches 50% when the population size is about 54K (and 77K for any int). See Birthday Problem Probability Table for collision probabilities of various hash code sizes.

    Also, your idea to use Math.abs() alone is flawed: It does not always return a positive number! In 2's compliment arithmetic, the absolute value of Integer.MIN_VALUE is itself! Famously, the hash code of "polygenelubricants" is this value.

    0 讨论(0)
  • 2020-12-17 21:07

    You already can get two strings with the same hashcode. This should be obvious if you think that you have an infinite number of strings and only 2^32 possible hashcodes.

    You just make it a little more probable when taking the absolute value. The risk is small but if you need an unique id, this isn't the right approach.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-17 21:24

    What you can do when you only have 30-50 values as you said is register each String you get into an HashMap together with a running counter as value:

    HashMap StringMap = new HashMap<String,Integer>();
    
    StringMap.add("Test",1);
    StringMap.add("AnotherTest",2);
    

    You can then get your unique ID by calling this:

    StringMap.get("Test"); //returns 1
    
    0 讨论(0)
提交回复
热议问题