因为公司的单个业务数据达到千W级别,并且有源源不断的新数据进来。新数据进来都需要进行查重,重复数据不能进来,查重条件有很多字符串的对比,最大的字符串不超过1000个字符,但是字符串的比较,对于数据库来说,非常的耗性能,如果能将String转成数字来进行比较对于性能的提高将非常有用。
后来想到String有一个hashcode,看看能否使用:
<SPAN style="FONT-SIZE: small">/**
* Returns a hash code for this string. The hash code for a
* <code>String</code> object is computed as
* <blockquote><pre>
* s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
* </pre></blockquote>
* using <code>int</code> arithmetic, where <code>s[i]</code> is the
* <i>i</i>th character of the string, <code>n</code> is the length of
* the string, and <code>^</code> indicates exponentiation.
* (The hash value of the empty string is zero.)
*
* @return a hash code value for this object.
*/
public int hashCode() {
int h = hash;
int len = count;
if (h == 0 && len > 0) {
int off = offset;
char val[] = value;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}</SPAN>
但是遗憾的是int的范围非常窄(-2147483648——2147483647),出现重复的概率虽然说比较低,但是不可避免会出现重复的可能性。
于是想到,是否可以自己重新写过hashcode方法,将int的范围扩大到long类型(-9223372036854774808~9223372036854774807),这样在目前长度的字符生成的hashcode出现重复的可能性应该几乎为零。
大家对于这个设想是否有什么更好的意见?