HashCode计算方法
程序员文章站
2024-03-22 18:03:04
...
Returns a hash code for this string. The hash code for a String
object is computed as
usings[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
int
arithmetic, where s[i]
is the ith character of the string, n
is the length of the
string, and ^
indicates exponentiation. (The hash value of the
empty string is zero.)
Overrides: hashCode()
in Object
- Returns:
- a hash code value for this object.
-
package com.datastruct.sort; import junit.framework.TestCase; public class AscIITest extends TestCase { int hash=0; int offset=0; char[] value={'s','s'}; public void test(){ System.out.println(this.hashCode()); } public int hashCode() { int h = hash; if (h == 0) { int off = offset; char val[] = value; int len = value.length; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; System.out.println("第"+(i+1)+"次"+h); } hash = h; } return h; } }