• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap initialization

 
Tom Panahil
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ramasubbhu Allur Kuppusamy
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
What was the solution, finally?
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic