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
posted
0
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
posted
0
once you Execute the ResultSet, you can check the size of records.Right!
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
posted
0
Tom, What was the solution, finally?
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
posted
0
Originally posted by vidya sagar: once you Execute the ResultSet, you can check the size of records.Right!
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.<br /> <br />#:^P
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.