-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathHashTable.java
More file actions
54 lines (46 loc) · 1.09 KB
/
HashTable.java
File metadata and controls
54 lines (46 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package ch15;
import java.math.BigInteger;
public class HashTable {
private Info[] arr;
/**
* 默认的构造方法
*/
public HashTable() {
arr = new Info[100];
}
/**
* 指定数组初始化大小
*/
public HashTable(int maxSize) {
arr = new Info[maxSize];
}
/**
* 插入数据
*/
public void insert(Info info) {
arr[hashCode(info.getKey())] = info;
}
/**
* 查找数据
*/
public Info find(String key) {
return arr[hashCode(key)];
}
public int hashCode(String key) {
// int hashVal = 0;
// for(int i = key.length() - 1; i >= 0; i--) {
// int letter = key.charAt(i) - 96;
// hashVal += letter;
// }
// return hashVal;
BigInteger hashVal = new BigInteger("0");
BigInteger pow27 = new BigInteger("1");
for(int i = key.length() - 1; i >= 0; i--) {
int letter = key.charAt(i) - 96;
BigInteger letterB = new BigInteger(String.valueOf(letter));
hashVal = hashVal.add(letterB.multiply(pow27));
pow27 = pow27.multiply(new BigInteger(String.valueOf(27)));
}
return hashVal.mod(new BigInteger(String.valueOf(arr.length))).intValue();
}
}