- 浏览: 227180 次
- 性别:
- 来自: 深圳
文章分类
- 全部博客 (96)
- J2EE (11)
- Spring (11)
- Oracle (3)
- Struts2 (7)
- Hibernate (1)
- Struts1.x (0)
- 职场内外 (1)
- Java (31)
- JSP基础 (3)
- 英语点滴 (1)
- Linux (2)
- JavaScript (12)
- ANT (5)
- 计算机基础知识 (2)
- 开发工具 (4)
- Java网络编程 (3)
- Swing (2)
- JVM (3)
- 网络协议 (3)
- 算法与数据结构 (5)
- Ext JS (1)
- Spring JUnit (1)
- XML (1)
- 搜索引擎 (1)
- NoSQL (1)
- 搜索(引擎) Solr Lucene (2)
- UML (1)
- Design Pattern (1)
最新评论
-
chokee:
struts2如何获取页面参数 -
mengfei86:
顶顶!
jstat的使用方法 -- 分析JVM的使用情况 -
yifzeng:
感谢楼上两位!!!
Struts2 验证框架不起作用的几种可能[转] -
xianqi_h:
如果所要用的形容词是几个,而且是上述所列的归类之外的词,如: ...
英语形容词的排列顺序 -- 重点记忆 -
skzr.org:
fixbug——在IE7下面:IE无法打开站点XXXX,无法操 ...
struts2开发中ie无法打开XXX站点 已终止操作
/**
* Hash算法大全<br>
* 推荐使用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, int prime)
{
int hash, i;
for (hash=key.length(), i=0; i<key.length(); ++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<key.length(); ++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<key.length(); ++i) hash = 33*hash + key.charAt(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<len; ++i)
// hash=tab[hash^key[i]];
// return (hash);
// }
/**///// CRC Hashing,计算crc,具体代码见其他
// ub4 crc(char *key, ub4 len, ub4 mask, ub4 tab[256])
// {
// ub4 hash, i;
// for (hash=len, i=0; i<len; ++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<(len<<3); i+=8)
{
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 mask, int[][] tab)
{
int hash, i;
for (hash=key.length, i=0; i<key.length; ++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<data.length();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;
}
}
发表评论
-
运行mvn eclipse:eclipse 报插件不存在或者版本不对错误
2013-07-26 20:59 1150我的maven版本是2.2.1,当我使用mvn eclip ... -
eclipse 安装velocity插件
2013-07-14 21:09 894安装velocity的eclipse插件步骤: ... -
java日志,需要知道的几件事 -- 转
2013-03-07 14:16 888http://blog.csdn.net/su ... -
构建高可扩Web架构和分布式系统实战 -- 转
2013-01-24 11:34 996http://www.csdn.net/articl ... -
Java中的ReentrantLock和synchronized两种锁定机制的对比 -- 转
2013-01-07 09:45 1170阅读本文,使我从对比的角度重新审视了两种锁的机制。 分享 ... -
ant jsch.jar -- 一个异常及解决方法
2012-12-11 14:49 3426运行build.xml出现异常: Cause: Cou ... -
struts2 拦截器 -- 转
2012-11-20 15:46 842深入认识struts2拦截器(甚至是struts2的ac ... -
web.xml文件中配置(servlet, spring, filter, listenr)的加载顺序 (转)
2012-11-20 14:44 834servlet: 项目的运 ... -
spring 事务架构 - Spring Transaction Architecture
2012-10-17 09:15 1438Spring Transaction Architecture ... -
全局事务与本地事务的区别应用
2012-10-15 15:44 1291http://blog.csdn.net/sunitjy ... -
搜索服务器Solr部署使用
2012-09-21 10:56 983本文肤浅探讨MySQL + Solr + Solr ... -
JAVA - DOM4J - XML 转化成String字串
2012-09-07 10:20 5321import org.dom4j.Document; imp ... -
JVM "Out of swap space"错误常见的原因
2012-08-01 15:31 1159Out of swap space error general ... -
struts2 converter 之类型转换器
2012-06-06 14:48 14951. 前面讲的自定义类型转换器是基于 OGNL 的 Defau ... -
Java中static、final用法小结
2012-05-18 14:02 1107一、final 1.final变量: 当 ... -
ConcurrentHashMap 详解
2012-05-15 15:03 0java.util.concurrent.Concurrent ... -
JAVA泛型简析
2012-05-11 15:13 1073泛型是JDK1.5支持的新功 ... -
Ant之build.xml详解
2012-05-08 14:55 1144Ant的概念 可能有些读 ... -
hashcode 与 equals 方法详解 --- 透彻,详尽
2012-01-15 20:41 0如果函数返回类型不同,子类的同名函数不能继承父类的同名函数 ... -
java中equals和==的区别
2012-01-15 19:03 787<script type="text/ja ...
相关推荐
采用java实现的常用hash算法归总。
java_Hash算法大全
工具类
MurmurHash算法由Austin Appleby创建于2008年,现已应用到Hadoop、libstdc 、nginx、libmemcached,Redis,Memcached,Cassandra,HBase,Lucene等开源系统。2011年Appleby被Google雇佣,随后Google推出其变种的...
一个hash算法的工具类,里面包含了一些常用的hash算法
算法学习:Hash算法大全,Java代码
Hash算法大全(java实现)参照.pdf
Hash算法大全(java实现)汇编.pdf
哈希计算工具 java-hash.7z
geohash基本原理是将地球理解为一个二维平面,将平面递归分解成更小的子块,每个子块在一定经纬度范围内拥有相同的编码。GeoHash将二维的经纬度转换成一维的字符串。
关于bin_sort、common、hash、red_black、quick_sort等算法的Java实现的源码,下载后可直接运行
java,算法,BD,bin_sort,common,dijkstra,hash,heap_sort , huffman, ins_sort ,huffman,ins_sort,matmult,mst,opt_bin,p_queue,q_sort,quick_sort,radix_sort,red_black,template,make_tar
最快的排序算法 javahash实现-Java-哈希算法-最快的实现,排序算法数据结构
网上有很多geohash算法的实现,都是基于java或者php代码实现的,没有sql实现的版本,这里使用mysql简单实现了这个算法
哈希计算工具 java-hash
主要介绍了Java常用HASH算法,结合实例形式总结分析了Java常用的Hash算法,包括加法hash、旋转hash、FNV算法、RS算法hash、PJW算法、ELF算法、BKDR算法、SDBM算法、DJB算法、DEK算法、AP算法等,需要的朋友可以参考下
非常使用的 基于geohash 找一定范围内的 最近位置java代码
ConsistentHash 一致性hash算法的 java 和 C++ 实现
加密加密算法调研以及 加密算法 代码实现,其中包括rsa、des、hash等算法
如果没有找到,则取整个环的第个节点。测试结果测试代码是整理的,主体法没有变分布平均性测试:测试随机成的众多key是否会平均分布到各个结点上测试结果如下:最上是参