• 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

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic