• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

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

 
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 Java Spring
  • 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
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 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: 64493
225
  • 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
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • 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: 64493
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!