HashCode 계산 방법

1420 단어 JUnit
Returns a hash code for this string. The hash code for a String object is computed as
 s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
 

using 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;
		    }
}

좋은 웹페이지 즐겨찾기