hashcode简单学习
来源:互联网 发布:东方财富软件使用 编辑:程序博客网 时间:2024/06/06 03:22
String 的hashcode方法:
String str = "abcdef";
h = 31 * (31* (31 * (31 * (31 * (31 * (0) + 'a') + 'b') + 'c') + 'd') + 'e')+'f';
自己写了一个测试程序验证改hash算法对不对:
package Example_4_3;public class Example4_3_1 { public static void main(String[] args) { System.out.println("abcdef.hashcode = " + "abcdef".hashCode()); System.out .println("abcdef.owner = "+ (31 * (31 * (31 * (31 * (31 * (31 * (0) + 'a') + 'b') + 'c') + 'd') + 'e') + 'f')); }}
最后运行一下之后结果如下:
abcdef.hashcode = -1424385949
abcdef.owner = -1424385949
由此可以看出String类的hashcode算法就是以31为基数,
基数的选择规律:必须是质数,因为质数只能被1和本身整除,这样任何数乘以一个整数更容易产生唯一性,不会被重复,减少散列(hashcode值)的冲突。
至于为什么选择31,而不是其他数字,是观测结果后的一个选择,原因不明,也有人反对使用31,认为数字太小,这样更容易产生冲突,需要选择更大的质数。
阅读全文
0 0
- hashcode()方法简单学习
- hashcode简单学习
- HashCode学习
- HashCode学习
- HashCode简单用法
- hashCode 初步学习
- HashCode学习笔记
- hashcode的简单简析
- hashcode 与 equals学习(一)
- hashcode 与 equals 学习(二)
- Java集合学习之hashCode
- 改写equals与hashCode的简单方法
- hashcode
- Hashcode
- hashCode()
- hashCode
- HashCode
- hashCode
- 神州电脑安装docker for Windows
- Mac改变launch的行列
- Pose-Aware Face Recognition in the Wild--填坑1
- android 异步线程的使用
- opencv学习笔记—播放AVI视频(下)
- hashcode简单学习
- HTML基础学习-2-文本标记学习
- kotlin学习2(第一个hello word 程序)
- 深度优先搜索版的匈牙利算法
- 略讲基于cnn的人脸识别模型的网络结构和度量设计
- 355Design Twitter
- 单片机学习——LCD12864串口显示
- UVa156(map的使用)
- hihoCoder— 穿越禁区(并查集)