赞
踩
Sring类重写了Object类的equals()方法和hashcode()方法。hashcode()方法的源代码如下:
- 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;
- }
- static void hashcodeTest(){
-
- String str = "yangcq";
-
- // 第一步 = (int)'y'
- // 第二步 = (31 * (int)'y') + (int)'a'
- // 第三步 = 31 * ((31 * (int)'y') + (int)'a') + (int)'n'
- // 第四步 = 31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g'
- // 第五步 = 31 * (31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g') + (int)'c'
- // 第六步 = 31 * (31 * (31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g') + (int)'c') + (int)'q'
-
- // 上面的过程,也可以用下面的方式表示
-
- // 第一步 = (int)'y'
- // 第二步 = 31 * (第一步的计算结果) + (int)'a'
- // 第三步 = 31 * (第二步的计算结果) + (int)'n'
- // 第四步 = 31 * (第三步的计算结果) + (int)'g'
- // 第五步 = 31 * (第四步的计算结果) + (int)'c'
- // 第六步 = 31 * (第五步的计算结果) + (int)'q'
-
- int hashcode = 31 * (31 * (31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g') + (int)'c') + (int)'q';
- System.out.println("yangcq的hashcode = " + hashcode); // yangcq的hashcode = -737879313
- System.out.println("yangcq的hashcode = " + str.hashCode()); // yangcq的hashcode = -737879313
-
- }

- /**
- * java中对象的hashcode值,是如何计算得到的。
- */
- public class MyHashcode {
-
- /** The value is used for character storage. */
- static char value[];
-
- /** The offset is the first index of the storage that is used. */
- static int offset;
-
- /** The count is the number of characters in the String. */
- static int count;
-
- /** Cache the hash code for the string */
- static int hash; // Default to 0
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String str1 = new String("yangcq");
- String str2 = new String("yangcq");
- // 如果2个字符串的内容相同,那么这2个字符串的hashcode必然相同
- System.out.println(new String("yangcq").hashCode() == new String("yangcq").hashCode());
- System.out.println(str1.hashCode() == str2.hashCode());
-
- System.out.println(str1.hashCode());
- System.out.println(hashCode1(str1));
-
- // 测试自定义的hashcode方法
- HashcodeOfString hashcodeOfString = new HashcodeOfString();
- hashcodeOfString.hashCode(str1);
- System.out.println("str1的hashcode = " + hashcodeOfString.hashCode(str1));
- System.out.println("str1的hashcode = " + str1.hashCode());
- }
-
- // HashMap中实现的hash算法(再hash算法)
- static int hash(int h) {
- // This function ensures that hashCodes that differ only by
- // constant multiples at each bit position have a bounded
- // number of collisions (approximately 8 at default load factor).
- h ^= (h >>> 20) ^ (h >>> 12);
- return h ^ (h >>> 7) ^ (h >>> 4);
- }
-
- // String类实现的hashcode方法源代码
- static int hashCode1(String str) {
- int h = hash;
- if (h == 0) {
- int off = 0;
- char val[] = str.toCharArray();
- int len = str.length();
-
- for (int i = 0; i < len; i++) {
- h = 31 * h + val[off++];
- }
- hash = h;
- }
- return h;
- }
-
- // String类的hashcode值(哈希值)是如何计算得到的?具体实现?为了方便阅读,我们来进行分步说明
- static void hashcodeTest(){
-
- String str = "yangcq";
-
- // 第一步 = (int)'y'
- // 第二步 = (31 * (int)'y') + (int)'a'
- // 第三步 = 31 * ((31 * (int)'y') + (int)'a') + (int)'n'
- // 第四步 = 31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g'
- // 第五步 = 31 * (31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g') + (int)'c'
- // 第六步 = 31 * (31 * (31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g') + (int)'c') + (int)'q'
-
- // 上面的过程,也可以用下面的方式表示
-
- // 第一步 = (int)'y'
- // 第二步 = 31 * (第一步的计算结果) + (int)'a'
- // 第三步 = 31 * (第二步的计算结果) + (int)'n'
- // 第四步 = 31 * (第三步的计算结果) + (int)'g'
- // 第五步 = 31 * (第四步的计算结果) + (int)'c'
- // 第六步 = 31 * (第五步的计算结果) + (int)'q'
-
- int hashcode = 31 * (31 * (31 * (31 * ((31 * (int)'y') + (int)'a') + (int)'n') + (int)'g') + (int)'c') + (int)'q';
- System.out.println("yangcq的hashcode = " + hashcode); // yangcq的hashcode = -737879313
- System.out.println("yangcq的hashcode = " + str.hashCode()); // yangcq的hashcode = -737879313
-
- }
- }

- /**
- *
- * @yangcq
- * @描述:String类的hashcode值是如何计算得到的。
- * 参考String类的hashcode()方法,写一个方法,计算任意字符串的hashcode
- *
- * 哈希值的特点:
- * 1,一定程度的唯一性;
- * 2,长度固定;
- * 3,哈希值是二进制值;
- * 4,要找到生成相同哈希值的2个不同输入,在有限时间内是不可能的;哈希算法应用于加密领域的原因。
- *
- */
- public class HashcodeOfString implements java.io.Serializable{
-
- private static final long serialVersionUID = -4208161728160233397L;
-
- public int hashCode(String str) {
- // 最终计算得出的哈希值,转化为int以后的哈希值
- int hashcode = 0;
- // 临时哈希值变量
- int hash = 0;
- if (hash == 0) {
- // 当前char的索引
- int off = 0;
- // 字符串str的字符数组表示
- char val[] = str.toCharArray();
- // 字符串str的长度
- int len = str.length();
- for (int i = 0; i < len; i++) {
- hash = 31 * hash + val[off++];
- }
- hashcode = hash;
- }
- return hashcode;
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。