http://www.cs.cmu.edu/~adamchik/15-121/lectures/Hashing/hashing.html
如何加快搜索的速度呢? 如果搜索一个unsorted array, 那么需要扫一遍array, O(n). 如果是sorted array, 则可以用binary search, O(log n).
如果事先知道要找的value分布在array的什么位置(index)的话, 我们可以在O(1)的时间找到这个value, Hash Function 就是这样一种方法.
Hash function has the following properties
使用Hashing Function的步骤(procedure):
Create an array of size M. Choose a hash function h, that is a mapping from objects into integers 0, 1, 2, ... , M - 1. Put these objects into an array at indexes computed via the hash function index = h(object). Such array is called a hash table.
如何来选择hash function呢? 一种方法是使用Java的 hashCode() 方法, which is implemented in the Object class and there fore each class in Java inherits it. 它可以对object提供一种数字化的表示 numeric representation, (相对于 toString() 方法会提供object的text representation)
例子:
hashCode() 方法对不同的class有不同的实现方式, 对于String来说, 如下:
s.charAt(0) * 31n-1 + s.charAt(1) * 31n-2 + ... + s.charAt(n-1)
s is a string and n is the length. Example:
"ABC" = 'A' * 312 + 'B' * 31 + 'C' = 65 * 312 + 66 * 31 + 67 = 64578
Note that Java's hashCode method might return a negative integer. If a string is long enough, its hashcode will be bigger than the largest integer we can store on 32 bits CPU. In this case, due to integer overflow, the value returned by hashCode can be negative.
Review the code in HashCodeDemo.java.