File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to fix the size of  a hash table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to fix the size of  a hash table" Watch "How to fix the size of  a hash table" New topic
Author

How to fix the size of a hash table

Supraja Jayakumar
Greenhorn

Joined: Jul 12, 2011
Posts: 7
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30309
    
150

Maybe create a wrapper for the hashtable (or utility function you can call) that keeps track of this information?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

For what reason?
Supraja Jayakumar
Greenhorn

Joined: Jul 12, 2011
Posts: 7
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

Joined: Mar 17, 2011
Posts: 7649
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
 
subject: How to fix the size of a hash table