How to generate a unique hash code for string input in android…?

前端 未结 8 511
迷失自我
迷失自我 2020-12-13 03:41

I wanted to generate a unique hash code for a string in put in android. Is there any predefined library is there or we have to generate manually. Please any body if knows pl

8条回答
  •  粉色の甜心
    2020-12-13 04:13

    Let's take a look at the stock hashCode() method:

    public int hashCode() {
        int h = hash;
        if (h == 0 && count > 0) {
            for (int i = 0; i < count; i++) {
                h = 31 * h + charAt(i);
            }
            hash = h;
        }
        return h;
    }
    

    The block of code above comes from the java.lang.String class. As you can see it is a 32 bit hash code which fair enough if you are using it on a small scale of data. If you are looking for hash code with more than 32 bit, you might wanna checkout this link: http://www.javamex.com/tutorials/collections/strong_hash_code_implementation.shtml

提交回复
热议问题