Hashcode Of A String In Java

Many of the Java programmers know what ‘Hashcode‘ means, but don‘t really know how exactly it is calculated and why 31 is used to calculate the hashcode. Below is the code snippet from Java 1.6, which calculates the hashcode for a string:

public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;

for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}

return h;
}

After reading up a bit, I wrote a sample test Java program, to find the hashcode of a string by multiplying by 31 (which is the same as shifting left (bitwise) by 5 times and subtracting, as in (i << 5) - i). Below is the sample test program:

public class TestHash {
public static void main(String[] args) {
String str1 = "What the heck?";

int hashcode1 = 0;
int hashcode2 = 0;

for(int i=0;i<str1.length();i++) {
hashcode1 = 31*hashcode1 + str1.charAt(i);
hashcode2 = (hashcode2 << 5) - hashcode2 + str1.charAt(i);
}

System.out.println("Hashcode1 : " + hashcode1);
System.out.println("Hashcode2 : " + hashcode2);
}
}

The output for this program is:

Hashcode1 : 277800975
Hashcode2 : 277800975

1、这段代码究竟是什么意思?

Even if someone knows why 31 is used, there is a lot of stuff to know about ‘Hashing‘, ‘Hash Collisions‘ and multiple algorithms related to calculating hash values. First off, its a known fact that there is no perfect hashing algorithm, for which there are no collisions. But there are several algorithms, which minimize the collisions and are good enough to use. Now, coming to why 31 is used in calculating hashcode, this is the reason given by Joshua Bloch, in the book ‘Effective Java‘:

《Effective Java》是这样说的:之所以选择31,是因为它是个奇素数,如果乘数是偶数,并且乘法溢出的话,信息就会丢失,因为与2相乘等价于移位运算。使用素数的好处并不是很明显,但是习惯上都使用素数来计算散列结果。31有个很好的特性,就是用移位和减法来代替乘法,可以得到更好的性能:31*i==(i<<5)-i。现在的VM可以自动完成这种优化。

2、它返回的hashCode有什么特点呢?

可以看到,String类是用它的value值作为参数来计算hashCode的,也就是说,相同的value就一定会有相同的hashCode值。这点也很容易理解,因为value值相同,那么用equals比较也是相等的,equals方法比较相等,则hashCode一定相等。反过来不一定成立。它不保证相同的hashCode一定有相同的对象。

一个好的hash函数应该是这样的:为不相同的对象产生不相等的hashCode。

在理想情况下,hash函数应该把集合中不相等的实例均匀分布到所有可能的hashCode上,要想达到这种理想情形是非常困难的,至少java没有达到。因为我们可以看到,hashCode是非随机生成的,它有一定的规律,就是上面的数学等式,我们可以构造一些具有相同hashCode但value值不一样的,比如说:Aa和BB的hashCode是一样的。

说到这里,你可能会想,原来构造hash冲突那么简单啊,那我是不是可以对HashMap函数构造很多<key,value>不都一样,但具有相同的hashCode,这样的话可以把HashMap函数变成一条单向链表,运行时间由线性变为平方级呢?虽然HashMap重写的hashCode方法比String类的要复杂些,但理论上说是可以这么做的。这也是最近比较热门的Hash Collision DoS事件。

HashMap里重写的hashCode方法

       public final int hashCode() {
           return (key==null   ? 0 : key.hashCode()) ^
                   (value==null ? 0 : value.hashCode());
        }

reference:

http://crd1991.iteye.com/blog/1473108

http://java-bytes.blogspot.com/2009/10/hashcode-of-string-in-java.html

时间: 2024-08-06 13:55:30

Hashcode Of A String In Java的相关文章

黑马程序员——hashCode方法的作用,java内存泄露

hashCode方法的作用:当有一个对象要存入hash集合的时候,JVM首先会调用hashCode方法获取该对象的哈希值,然后根据哈希值找到相应的存储区域,最后取出该区域的所有元素与该对象进行equals比较,如果相等,不存入该元素,否则,存入.这样不用遍历集合中的所有元素就能的到我们想要的结果,提高了查找的效率.但是如果不覆写hashCode方法的话,相同的对象可能会存储在HashSet集合中,虽然他们equals比较相同,但他们的内存区域不同的话,就不会进行equals比较了.为了让两个相同

String是java中的基本数据类型吗

1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. Java代码 new String() 和 new String("")都是申明一个新的空字符串,是空串不是null: 3. String str="kvill": String str=new String ("kvill"); 的区别: 在这里,我们

警告: Can&#39;t transform property &#39;acceptEvent&#39; from java.lang.String into java.util.List. Will register

警告: Can't transform property 'acceptEvent' from java.lang.String into java.util.List. Will register a default Morpher 产生错误的代码: import net.sf.json.JSONObject; public static <T> T jsonToObject(String jsonString, Class<T> pojoCalss) { try{ Object

Reverse Words in a String leetcode java

题目: Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". click to show clarification. Clarification: What constitutes a word? A sequence of non-space characters con

Scramble String leetcode java

题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choo

Interleaving String leetcode java

题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return true. When s3 = "aadbbbaccc", return false. 题解: 这道题还是像

三、为什么String在Java中是不可更改的

String在Java中是个不可更改的类.一个不可更改的类简单来说就是这个类的所有实例是不可以更改的.所有的实例信息在创建的时候被初始化而且信息是不可以更改的.不可更改的类有很多好处.这篇文章总结了为什么String被设计成不可以改变的.一个好的回答需要深入理解内存.同步和数据结构等.1. 字符串池的需要字符串池(字符串内部池) 是在方法区域的特殊区域.当一个string被创建如果这个string已经在内存里面存在了,那个存在的string的引用被返回,而不是创建个新的对象和返回它的引用.下面的

[乐意黎原创] glassfish里抛出Note: string:///XXX_jsp.java from uses unchecked or unsafe operations.

glassfish里抛出如下错误: org.apache.jasper.JasperException: PWC6033: Unable to compile class for JSP PWC6199: Generated servlet error: string:///XXXX_jsp.java:169: constant string too long PWC6199: Generated servlet error: Note: string:///XXXX_jsp.java from

Attempt to invoke virtual method &#39;boolean java.lang.String.equals(java.lang.Object)&#39; on a null object reference xxx 的问题分析与解决方案

最近,公司要求开发一个APP,所以很苦逼的学习了几天 Android 的上手手册,但是实际运用于开发中还是捉襟见肘,困难重重:好在的是,部门还有几个专门搞安卓的大佬可以问问,哈哈 好了,进入今天的正题吧,前天开发过程中,遇到一个如“标题”所示的问题,百思不得其解!最终上网搜报错的原因,才找到问题的所在:.xml文件中运用到了不存在的标签--我的是因为粗心把<View>写成了<view>,加载的时候找不到该标签,所以页面一直运行不出来. 所以,该问题基本出在于布局页面有错(有的错误页