This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have begun writing a simple class which purpose is to query one database table based on the result queried from another table. I have taken the approach to use a hashmap (more flexible since data can be VERY large in occasions) and I am getting a strange behavior.
NB: The class I pasted only shows the first part of my work ; Once I can populate my hashmap correctly, I plan to use a PreparedStatement() to dynamically generate a second query to retrieve results from a much larger table.
The problem is that I only obtain 1 value in the hashmap I use instead of eight (as expected) ; custidHM.size() returns "1".
Running my class under debugger shows me that the put() statement works but "overwrites" the previous value. Using a for() loop has no effect. I am sure my mistake is a stupid one. Can someone point it out please? I don't see it...
TIA to anyone for their time.
Alan. [ September 18, 2006: Message edited by: Alan Christen ]
A HashMap associates just one value with each key. In your loop, the key is the same each time (the name of your single column). Therefore each new value will replace the last.
A HashMap is for looking up things, where you have one piece of data (the key), and want to find some other data associated with it (the value). That doesn't describe anything you'd want to do here: all the values have the same "key", so there's no point in using a Map. Instead, you could just store the column values in a List.
You just need to think about what you'd use this data structure for. You could use a HashMap to store a single row, with the key for each value being the column name, and they values being the contents of the table cell for that column. Alternately, you could store, say, usernames and passwords, with the usernames being the keys and the passwords being the values.
There are a number of other things going on here that I could criticize, but I'm going to leave it to just one. Don't call "registerDriver()" -- the driver itself will call that automatically. All you need to do is load the driver class, which is usually done using Class.forName():
The advantage being that you can move that class name into a configuration parameter, and then change databases at runtime without modifying your code.
Both ArrayList and HashMap have unlimited size - which is to say the class itself doesn't have a hard upper limit (well I guess they do have a limit of MAXINT (2^31-1) or I don't know what happens after that as they both have size() which returns int). Of course both will be limited by available memory. A HashMap is likely to take more memory than an ArrayList simply because it has 2 objects for every entry - Key and Value versus one for ArrayList - value.
You almost certainly don't want to try to hold 650 Million objects in memory at once. Even if you were storing primitive ints thats 32 bits per:
Thats 20800000000 bits or 2600000000 bytes - thats 2479.55 MB or 2.42 GB. [ September 18, 2006: Message edited by: Tim LeMaster ]