File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how to append the values retrieved from the database to the hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to append the values retrieved from the database to the hashmap" Watch "how to append the values retrieved from the database to the hashmap" New topic
Author

how to append the values retrieved from the database to the hashmap

Abhijit Ajansondkar
Greenhorn

Joined: Jul 13, 2012
Posts: 10
Hi, I have retrieved values from database .Now I want to append those values to the hashmap. So please suggest me the logic. This is my code .
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


A Hashmap stores values as [key - value] pairs, where distinct keys act as 'references' for the values stored.

You should decide how you want to store the values. Usually for storing an object in a Map we store it as the value along with the ID for this element as the key.
i.e. hashmap.add(id, object);

Why is the requirement that the elements have to be stored in a Map? As they appear to be a heterogeneous collection I would think of a different collection (unless they are connected in some different way).

Cheers,
Rajkamal.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3602
    
  15
You can't append to a HashMap. They have no order so appending is meaningless.
You can add to a HashMap. The best way to do this would be to create a class to hold all the data you retrieve from the database, create an instance of this class for each batch of data that you retrieve and then put this instance into the HashMap using a suitable key value which needs to be unique.


Joanne
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 983
    
  13

A couple of tips. Don't return a HashMap, return a Map. This makes it easier to change the implementation later to say, a TreeMap.

You are currently creating a new HashMap for every record. I'm pretty sure that's not what you want. Move that outside of the loop.

Move your return outside the loop unless you want to return after only one record.

Finally, I would create a bean to represent your records. Populate a bean for each record and then add those beans to a List or Map, whichever is appropriate for your requirements.


"The good news about computers is that they do what you tell them to do. The bad news is that they do what you tell them to do." -- Ted Nelson
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Jk Robbins wrote: . . . create a bean to represent your records. Populate a bean for each record and then add those beans to a List or Map, whichever is appropriate for your requirements.
Good idea, but I think maybe you should explain a little about what a bean is; OP might not have heard of it.
J. Kevin Robbins
Bartender

Joined: Dec 16, 2010
Posts: 983
    
  13

Good point. Joanne mentioned creating a class to hold the data. That's what a bean (JavaBean) is. It contains fields and setters and getters, nothing else. In this case it would look something like this:



In the loop you'll do something like this:


I may have already told you more than I should have, so I'll stop here and let you take your next shot at it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Thank you very much.
 
GeeCON Prague 2014
 
subject: how to append the values retrieved from the database to the hashmap