• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 450
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 65448
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 65448
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!