简体   繁体   中英

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.

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. 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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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