标签:索引 计算机 身份证 ret shc this first strong 简单
哈希函数的设计:
1.键通过哈希函数得到索引分布均匀:特殊领域的哈希函数设计方式甚至有专门的论文这个不考虑,我们只是设计最普通的函数设计:
哈希函数总结:上述设计方式都是转化成整型处理,并不是唯一的方法,
原则:
1.一致性:如果a == b,则hash(a) == hash(b)
2.高效性:计算高效简便
3.均匀性:哈希值均匀分布
2:JAVA中的hashCode方法:
package hash; /** * Created by admin on 2019/1/10. */ public class HashCode { public static void main(String[] args) { Integer a = 42; System.out.println(a.hashCode()); Integer b = -42; System.out.println(b.hashCode()); Double c = 3.141592653; System.out.println(c.hashCode()); String d = "airycode"; System.out.println(d.hashCode()); Student s = new Student(3,2,"bobo","bobo"); System.out.println(s.hashCode()); Student s2 = new Student(3,2,"BoBo","BoBo"); System.out.println(s2.hashCode()); } } package hash; /** * Created by admin on 2019/1/10. */ public class Student { int grade; int cls; String firstName; String lastName; public Student(int grade, int cls, String firstName, String lastName) { this.grade = grade; this.cls = cls; this.firstName = firstName; this.lastName = lastName; } @Override public int hashCode() { int B = 31; int hash = 0; hash = hash*B+grade; hash = hash*B+cls; hash = hash*B+firstName.toLowerCase().hashCode();//BB 和bb是一个人 hash = hash*B+lastName.toLowerCase().hashCode(); return hash; } }
3:哈希函数冲突的处理:链地址法
java8之前,哈希表的实现,每个位置上对应的是一个链表。java8之后,数据量小的时候,还是链表,当哈希冲突达到一定的程度每个位置从链表转成红黑树。
标签:索引 计算机 身份证 ret shc this first strong 简单
原文地址:https://www.cnblogs.com/airycode/p/10249709.html