1

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.

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
user1838343
  • 451
  • 2
  • 8
  • 16

1 Answers1

0

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

Community
  • 1
  • 1
Sheel
  • 847
  • 2
  • 8
  • 20
  • @user1838343 I think it will be ok you can also modify your load factor you can find useful link here [Hashfunction](http://en.wikipedia.org/wiki/Hash_function) – Sheel Aug 03 '13 at 09:39
  • @user1838343if you are still n trouble then comment. – Sheel Aug 03 '13 at 09:52