本文实例讲述了Java常用HASH算法。分享给大家供大家参考,具体如下:

/**

* Hash算法大全

* 推荐使用FNV1算法

* @algorithm None

* @author Goodzzp 2006-11-20

* @lastEdit Goodzzp 2006-11-20

* @editDetail Create

*/

public class HashAlgorithms

{

/**//**

* 加法hash

* @param key 字符串

* @param prime 一个质数

* @return hash结果

*/

public static int additiveHash(String key,int prime)

{

int hash,i;

for (hash = key.length(),i = 0; i < key.length(); i++)

hash += key.charAt(i);

return (hash % prime);

}

/**//**

* 旋转hash

* @param key 输入字符串

* @param prime 质数

* @return hash值

*/

public static int rotatingHash(String key,i;

for (hash=key.length(),i=0; i

hash = (hash<<4)^(hash>>28)^key.charAt(i);

return (hash % prime);

// return (hash ^ (hash>>10) ^ (hash>>20));

}

// 替代:

// 使用:hash = (hash ^ (hash>>10) ^ (hash>>20)) & mask;

// 替代:hash %= prime;

/**//**

* MASK值,随便找一个值,最好是质数

*/

static int M_MASK = 0x8765fed1;

/**//**

* 一次一个hash

* @param key 输入字符串

* @return 输出hash值

*/

public static int oneByOneHash(String key)

{

int hash,i;

for (hash=0,i=0; i

{

hash += key.charAt(i);

hash += (hash << 10);

hash ^= (hash >> 6);

}

hash += (hash << 3);

hash ^= (hash >> 11);

hash += (hash << 15);

// return (hash & M_MASK);

return hash;

}

/**//**

* Bernstein's hash

* @param key 输入字节数组

* @param level 初始hash常量

* @return 结果hash

*/

public static int bernstein(String key)

{

int hash = 0;

int i;

for (i=0; i

return hash;

}

//

/**/ Pearson's Hash

// char pearson(char[]key,ub4 len,char tab[256])

// {

// char hash;

// ub4 i;

// for (hash=len,i=0; i

// hash=tab[hash^key[i]];

// return (hash);

// }

/**/ CRC Hashing,计算crc,具体代码见其他

// ub4 crc(char *key,ub4 mask,ub4 tab[256])

// {

// ub4 hash,i;

// for (hash=len,i=0; i

// hash = (hash >> 8) ^ tab[(hash & 0xff) ^ key[i]];

// return (hash & mask);

// }

/**//**

* Universal Hashing

*/

public static int universal(char[]key,int mask,int[] tab)

{

int hash = key.length,i,len = key.length;

for (i=0; i

{

char k = key[i>>3];

if ((k&0x01) == 0) hash ^= tab[i+0];

if ((k&0x02) == 0) hash ^= tab[i+1];

if ((k&0x04) == 0) hash ^= tab[i+2];

if ((k&0x08) == 0) hash ^= tab[i+3];

if ((k&0x10) == 0) hash ^= tab[i+4];

if ((k&0x20) == 0) hash ^= tab[i+5];

if ((k&0x40) == 0) hash ^= tab[i+6];

if ((k&0x80) == 0) hash ^= tab[i+7];

}

return (hash & mask);

}

/**//**

* Zobrist Hashing

*/

public static int zobrist( char[] key,int[][] tab)

{

int hash,i;

for (hash=key.length,i=0; i

hash ^= tab[i][key[i]];

return (hash & mask);

}

// LOOKUP3

// 见Bob Jenkins(3).c文件

// 32位FNV算法

static int M_SHIFT = 0;

/**//**

* 32位的FNV算法

* @param data 数组

* @return int值

*/

public static int FNVHash(byte[] data)

{

int hash = (int)2166136261L;

for(byte b : data)

hash = (hash * 16777619) ^ b;

if (M_SHIFT == 0)

return hash;

return (hash ^ (hash >> M_SHIFT)) & M_MASK;

}

/**//**

* 改进的32位FNV算法1

* @param data 数组

* @return int值

*/

public static int FNVHash1(byte[] data)

{

final int p = 16777619;

int hash = (int)2166136261L;

for(byte b:data)

hash = (hash ^ b) * p;

hash += hash << 13;

hash ^= hash >> 7;

hash += hash << 3;

hash ^= hash >> 17;

hash += hash << 5;

return hash;

}

/**//**

* 改进的32位FNV算法1

* @param data 字符串

* @return int值

*/

public static int FNVHash1(String data)

{

final int p = 16777619;

int hash = (int)2166136261L;

for(int i=0;i

hash = (hash ^ data.charAt(i)) * p;

hash += hash << 13;

hash ^= hash >> 7;

hash += hash << 3;

hash ^= hash >> 17;

hash += hash << 5;

return hash;

}

/**//**

* Thomas Wang的算法,整数hash

*/

public static int intHash(int key)

{

key += ~(key << 15);

key ^= (key >>> 10);

key += (key << 3);

key ^= (key >>> 6);

key += ~(key << 11);

key ^= (key >>> 16);

return key;

}

/**//**

* RS算法hash

* @param str 字符串

*/

public static int RSHash(String str)

{

int b = 378551;

int a = 63689;

int hash = 0;

for(int i = 0; i < str.length(); i++)

{

hash = hash * a + str.charAt(i);

a = a * b;

}

return (hash & 0x7FFFFFFF);

}

/**//* End Of RS Hash Function */

/**//**

* JS算法

*/

public static int JSHash(String str)

{

int hash = 1315423911;

for(int i = 0; i < str.length(); i++)

{

hash ^= ((hash << 5) + str.charAt(i) + (hash >> 2));

}

return (hash & 0x7FFFFFFF);

}

/**//* End Of JS Hash Function */

/**//**

* PJW算法

*/

public static int PJWHash(String str)

{

int BitsInUnsignedInt = 32;

int ThreeQuarters = (BitsInUnsignedInt * 3) / 4;

int OneEighth = BitsInUnsignedInt / 8;

int HighBits = 0xFFFFFFFF << (BitsInUnsignedInt - OneEighth);

int hash = 0;

int test = 0;

for(int i = 0; i < str.length();i++)

{

hash = (hash << OneEighth) + str.charAt(i);

if((test = hash & HighBits) != 0)

{

hash = (( hash ^ (test >> ThreeQuarters)) & (~HighBits));

}

}

return (hash & 0x7FFFFFFF);

}

/**//* End Of P. J. Weinberger Hash Function */

/**//**

* ELF算法

*/

public static int ELFHash(String str)

{

int hash = 0;

int x = 0;

for(int i = 0; i < str.length(); i++)

{

hash = (hash << 4) + str.charAt(i);

if((x = (int)(hash & 0xF0000000L)) != 0)

{

hash ^= (x >> 24);

hash &= ~x;

}

}

return (hash & 0x7FFFFFFF);

}

/**//* End Of ELF Hash Function */

/**//**

* BKDR算法

*/

public static int BKDRHash(String str)

{

int seed = 131; // 31 131 1313 13131 131313 etc..

int hash = 0;

for(int i = 0; i < str.length(); i++)

{

hash = (hash * seed) + str.charAt(i);

}

return (hash & 0x7FFFFFFF);

}

/**//* End Of BKDR Hash Function */

/**//**

* SDBM算法

*/

public static int SDBMHash(String str)

{

int hash = 0;

for(int i = 0; i < str.length(); i++)

{

hash = str.charAt(i) + (hash << 6) + (hash << 16) - hash;

}

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;

}

}

希望本文所述对大家java程序设计有所帮助。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

java字符串hash算法_Java常用HASH算法总结【经典实例】相关推荐

  1. java 字符串 面试题_Java常用类String的面试题汇总(java面试题)

    1.比较两个字符串时使用"=="还是equals()方法? 当然是equals方法."=="测试的是两个对象的引用是否相同,而equals()比较的是两个字符串 ...

  2. 我们一起来排序——使用Java语言优雅地实现常用排序算法

    破阵子·春景 燕子来时新社,梨花落后清明. 池上碧苔三四点,叶底黄鹂一两声.日长飞絮轻. 巧笑同桌伙伴,上学径里逢迎. 疑怪昨宵春梦好,元是今朝Offer拿.笑从双脸生. 排序算法--最基础的算法,互 ...

  3. java 性能 排序_Java常用排序算法及性能测试集合

    package algorithm.sort; import java.lang.reflect.Method; import java.util.Arrays; import java.util.D ...

  4. java常见的算法_Java常用算法总结(转)

    交换排序 冒泡排序 将最后一个元素与倒数第二个元素对比,如果最后一个元素比倒数第二个小,则交换两个元素的位置,再用倒数第二个元素与倒数第三个元数对比,直到比到第一个元素,这样经过第一趟排序后得到第一个 ...

  5. java gc回收算法_Java GC回收算法-判定一个对象是否可以回收

    开源推荐 推荐一款一站式性能监控工具(开源项目) Pepper-Metrics是跟一位同事一起开发的开源组件,主要功能是通过比较轻量的方式与常用开源组件(jedis/mybatis/motan/dub ...

  6. 6种java垃圾回收算法_Java垃圾回收算法

    主要根据以下3篇博客做的整理 http://blog.csdn.net/zsuguangh/article/details/6429592 http://www.cnblogs.com/ywl925/ ...

  7. java 蚁群算法_Java蚁群算法(Ant Colony)求解旅行商问题(TSP)(二)

    算法准备 旅行商问题(TSP)是一个经典的图论问题.在给定一系列城市和他们之间的距离以后,一个旅行商人希望能够找到一条能够走遍所有城市,并返回起点城市的最短路径.既然路径能串起来所有的城市,那么问题中 ...

  8. java 字符串编程题_Java编程题——在一个字符串中查找第一个非重复的字符

    编写一个Java程序来查找一个字符串中第一个非重复的字符,这是在编程测试中很常见的一个问题,因为字符串处理在程序员面试中是一个普遍的话题.面试前最好是准备好一些熟知的编程问题,例如使用递归反转字符串, ...

  9. 垃圾回收算法_Java 垃圾回收算法与几种垃圾回收器

    一.如何确定某个对象是"垃圾"? 目前主流垃圾回收器都采用的是可达性分析算法来判断对象是否已经存活,不使用引用计数算法判断对象时候存活的原因在于该算法很难解决相互引用的问题.如何确 ...

  10. matlab 图像白平衡算法,Matlab常用白平衡算法

    <Matlab常用白平衡算法>由会员分享,可在线阅读,更多相关<Matlab常用白平衡算法(21页珍藏版)>请在人人文库网上搜索. 1.1 灰色世界法灰色世界法(grey wo ...

最新文章

  1. vs2008\drivers\opengl_2.h.c_opengl基本功能介绍+示例
  2. linux进程管理之进程创建
  3. IP修改器的作用以及用途
  4. 1至m为PQ节点,m+1至n-1为PV节点,n为平衡节点
  5. java.net.URISyntaxException的解决办法
  6. 鸡啄米vc++2010系列2(项目文件分析)
  7. 如何安装php5.5,源码安装php5.5
  8. 地理必修一三大类岩石_高中地理 | 必备基础知识点干货
  9. python slice和列表切割_Python 列表切边 slice
  10. by mybatis 自定义order_MyBatis动态SQL实现ORDER BY和LIMIT的控制?
  11. 181016扇贝有道词霸每日一句
  12. c语言课后答案详解,c语言课后练习题答案详解_0.doc
  13. 成形滤波器和匹配滤波器
  14. 拼多多流量不精准是什么原因?怎么提高点击?
  15. 计算机网络(网络编程)
  16. word目录中有正文
  17. 密西西比河谷州立大学:Android应用程序开发(一)
  18. 次坐标从0开始_定位基础-坐标变换
  19. react-native 实现渐变色背景
  20. 编辑为什么建议转投_sci编辑建议转投应该接受吗

热门文章

  1. 台式电脑自带照片编辑软件将二寸照片改为一寸照片
  2. cvr存储服务器的优势,CVR是什么
  3. python Beautifulsoup4爬取凡人修仙传仙界篇连载中文章并生成txt
  4. 几何公差基础知识之圆度
  5. HP惠普笔记本Microsoft ACPI Compliant System未知设备的解决办法
  6. html创建站点文件夹,构建及访问Web站点
  7. c语言51单片机调节led亮度,51单片机中用PWM控制LED亮度调节
  8. cadence allegro - PCB线路敷铜渲染
  9. 浙江2段线能上什么计算机学校,二段线考生看过来!这些浙江省内热门高校还有热门专业可捡漏...
  10. Windows 使用命令行查看 wifi 密码