简体   繁体   English

具有散列值的自定义哈希表

[英]Custom hashtable with distibuted values

I have some integer value like: 我有一些整数值,例如:

0, 2, 3, 1021, 2001, 2101, 3054 ...

Now, I want to put those values in a hash table. 现在,我想将这些值放在哈希表中。 The integers are distributed like: every 1000 interval [means, 0-1000, 1000-2000 ...] has maximum 2-3 values. 整数的分布方式如下:每1000个间隔[平均值,0-1000、1000-2000 ...]具有2-3个最大值。

Now, in my hash table I'm simply setting the bucket number with load factor 0.5. 现在,在哈希表中,我只是将负载因子设置为存储桶编号。 And hash code is simply: Integer % bucket number. 哈希码很简单:整数%存储桶号。 However, it gives many collision. 但是,它会产生许多冲突。

Is there any better way to handle this type of particular distribution? 有没有更好的方法来处理这种特定的分布?

I have many files with such integers. 我有很多带有此类整数的文件。 So, setting fixed bucket number is impossible. 因此,不可能设置固定的存储桶编号。

@asslysis is saying you have to create a your own hashing function GoodHashingFunction Good Hashing Function is necessary because if it is any duplicate key it will replace value. @asslysis表示您必须创建自己的哈希函数GoodHashingFunction Good Hashing Function是必需的,因为如果它是任何重复的键,它将替换值。 See the code 看代码

import java.util.Hashtable;
public class HashMapKey {
public static void main(String[] args) {
    Hashtable a=new Hashtable();
    a.put("abc", 1000);
    a.put("abc", -1000);
    a.put("cde", 2000);
    System.out.println(a);
}

}

and output be {abc=-1000, cde=2000} 输出为{abc = -1000,cde = 2000}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM