• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to fix the size of a hash table

 
Supraja Jayakumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am trying to set an upper limit on the java hashmap. I am trying to test how many keys on average hash to the same location. (I think I should take a linked hash map?).
How to set this upper limit ?

Thanks
Supraja J
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe create a wrapper for the hashtable (or utility function you can call) that keeps track of this information?
 
Stephan van Hulst
Bartender
Posts: 6318
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what reason?
 
Supraja Jayakumar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A small correction to the question: I want to set an upperlimit on the size of the hashmap.

I have to collect some statistics for a class I am taking - about hash map implementations in java.

@Jeanne Can you please be more elaborate ?
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Supraja Jayakumar wrote:A small correction to the question: I want to set an upperlimit on the size of the hashmap.

Then strictly speaking I suspect you don't want to set an upper limit on the size of the HashMap; you want to set an upper limit on the number of buckets. And don't forget that HashMap actually applies a second hash to yours when it determines the bucket to put it in.

I have a feeling you might be better off using a TreeMap<Integer, Object> (or maybe a ConcurrentSkipListMap; it may be smaller) and simply adding objects and their hashcodes, and logging the times when your put() returns a non-null value. However, if your hash function is good, you may need an enormous number of objects before you get a collision. Alternatively, have a look at the HashMap source and duplicate its secondary hash for a particular size of "bucket map".

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic