This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Populating hashmap does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Populating hashmap does not work" Watch "Populating hashmap does not work" New topic

Populating hashmap does not work

Alan Christen

Joined: Sep 18, 2006
Posts: 16

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.

[ September 18, 2006: Message edited by: Alan Christen ]
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189


Welcome to JavaRanch!

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.

[Jess in Action][AskingGoodQuestions]
Alan Christen

Joined: Sep 18, 2006
Posts: 16

Thanks for taking the time to reply. I see my mistake now (smacking forehead...duh).

One quick question though: the reason why I chose Hashmap() is that it appeared to have large "Storage" capacity.

I am dealing with large tables (up to 650 million values in the worst case) ; could I use a list of store that many elements?

Previous attempts using arrays failed with "Java Heap Space" error.


Tim LeMaster
Ranch Hand

Joined: Aug 31, 2006
Posts: 226
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 ]
rocket mass heater kickstarter
subject: Populating hashmap does not work