【发布时间】:2015-04-11 19:45:10
【问题描述】:
每次发生冲突时,我都会尝试 rehash() 我的 HashTable,但我不断收到 Java 堆空间错误。
基本上,我有一个 String[] 表,每次我的哈希值发生冲突时,我都想将其长度乘以 2。
编辑:我在 while 循环中使用 insert(),它将大约 300.000 个单词加载到哈希表中。
public void rehash() {
String[] backup = table;
size = size * 2;
// i get the error on the line below
table = new String[size];
System.out.println("size" + size);
for (int i = 0; i < backup.length; i++) {
if (backup[i] != null) {
insert(backup[i]);
}
}
public void insert(String str) {
int index = hashFunction(str);
if (index > size || table[index] != null) {
rehash();
}
table[index] = str;
}
我的哈希函数:
int val= 0;
val= s.hashCode();
if (val< 0) {
val*= -1;
}
while (val> this.size) {
val%= this.size;
}
return val;
public void load() {
String str = null;
try {
BufferedReader in = new BufferedReader(new FileReader(location));
while ((str = in.readLine()) != null) {
insert(str);
}
in.close();
} catch (Exception e) {
System.out.println("exception");
}
}
【问题讨论】:
标签: java hash out-of-memory hashtable