• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Populating hashmap does not work

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings!

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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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():

Class.forName("oracle.jdbc.OracleDriver");

The advantage being that you can move that class name into a configuration parameter, and then change databases at runtime without modifying your code.
 
Alan Christen
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ernest,

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.

TIA,

Alan.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic