wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes HashMap initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap initialization" Watch "HashMap initialization" New topic
Author

HashMap initialization

Tom Panahil
Greenhorn

Joined: May 31, 2006
Posts: 2
Here's my situation -
I am loading data from a RDBMS and putting it into a hashmap for quick access in later processing. I get a ResultSet, iterate through, and populate my hash.

I want to init my HashMap so it is efficient... about the right size to avoid rehashing, but not bigger than I need. But, until I get through the ResultSet, I don't know how much stuff I am dealing with... 50 records, 100, 1000...

Any thoughts?
Ramasubbhu Allur Kuppusamy
Ranch Hand

Joined: Sep 16, 2005
Posts: 120
When you are iterating through the ResultSet and populating the HashMap as and when you encounter a new row, you should have no problems with the efficiency of HashMap.
As far as my lnowledge goes, HashMap's size is decided dynamically.


Regards,<br />Ram.<br />SCJP 1.4
vidya sagar
Ranch Hand

Joined: Mar 02, 2005
Posts: 580
once you Execute the ResultSet, you can check the size of records.Right!

Then construct the Hashmap with a capacity

HashMap objHM = new HashMap(<size of records> ;
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by vidya sagar:
once you Execute the ResultSet, you can check the size of records.Right!

Then construct the Hashmap with a capacity

HashMap objHM = new HashMap(<size of records> ;


That doesn't sound right. If the poster is thinking of moving to the bottom on the ResultSet and back to the top, that will probably take more time than using a forward-only record set and populating the Map in the simplest possible way.

To the original poster: don't sweat the efficiency until you discover it's a problem. How many rows are we talking about anyway? If this really is a concern (which I doubt), I would execute an efficient query written to *estimate* the size of your proper query. Then use that size to intial the capacity of the Map.


There is no emoticon for what I am feeling!
Ramasubbhu Allur Kuppusamy
Ranch Hand

Joined: Sep 16, 2005
Posts: 120
Tom,
What was the solution, finally?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by vidya sagar:
once you Execute the ResultSet, you can check the size of records.Right!

Then construct the Hashmap with a capacity

HashMap objHM = new HashMap(<size of records> ) ;


This isn't right, because the value taken by HashMap constructor is the size of its internal arrays, not the number of entries it can store. You need to supply a larger number. Read the API.

... or don't bother. In the vast majority of cases, just initialising the HashMap with the default constructor will perform fine.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap initialization