Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 32481
    
214

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


[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3986
    
  18

For what reason?


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
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: 8661
    
  23

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
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: How to fix the size of a hash table
 
jQuery in Action, 3rd edition