java Hash算法大全(三)

2014-11-24 02:47:54 · 作者: · 浏览: 2

return (hash & 0x7FFFFFFF);
}
/**//* End Of SDBM Hash Function */

/**//**
* DJB算法
*/
public static int DJBHash(String str)
{
int hash = 5381;

for(int i = 0; i < str.length(); i++)
{
hash = ((hash << 5) + hash) + str.charAt(i);
}

return (hash & 0x7FFFFFFF);
}
/**//* End Of DJB Hash Function */

/**//**
* DEK算法
*/
public static int DEKHash(String str)
{
int hash = str.length();

for(int i = 0; i < str.length(); i++)
{
hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
}

return (hash & 0x7FFFFFFF);
}
/**//* End Of DEK Hash Function */

/**//**
* AP算法
*/
public static int APHash(String str)
{
int hash = 0;

for(int i = 0; i < str.length(); i++)
{
hash ^= ((i & 1) == 0) ( (hash << 7) ^ str.charAt(i) ^ (hash >> 3)) :
(~((hash << 11) ^ str.charAt(i) ^ (hash >> 5)));
}

// return (hash & 0x7FFFFFFF);
return hash;
}
/**//* End Of AP Hash Function */

/**//**
* JAVA自己带的算法
*/
public static int java(String str)
{
int h = 0;
int off = 0;
int len = str.length();
for (int i = 0; i < len; i++)
{
h = 31 * h + str.charAt(off++);
}
return h;
}

/**//**
* 混合hash算法,输出64位的值
*/
public static long mixHash(String str)
{
long hash = str.hashCode();
hash <<= 32;
hash |= FNVHash1(str);
return hash;
}
}

作者“成长的日子”