Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to convert HashMap to JavaBean

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am searching how to convert a HashMap to a Javabean. Looking how to create a bean (whose String attributes match with HashMap keys ) given bean class as input along with hashmap.
I read that we can use Dozer or org.apache.commons.beanutils. Can any one please provide the best way to do this.

Thanks a lot..

regards,
achsu
 
Sheriff
Posts: 21940
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

achsu subra wrote:please assist ASAP.


Please EaseUp.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you have a look at Apache BeanUtils or Dozer?

I once wrote some code to view a Java Bean as a Map.
 
achsu subra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Did you have a look at Apache BeanUtils or Dozer?

I once wrote some code to view a Java Bean as a Map.



Hi Jesper,

Yes, I went through the org.apache.commons.beanutils. But I am not sure how I need use DynaBean or the BeanUtils class.
For e.g, I have hashmaps (key = value) as below-

attribute[name] = Joan
attribute[age] = 30

From this, I need generate the Bean with getter/setter methods for "name" & "age".

Please help, thanks a lot..

regards,
Achsu
 
Greenhorn
Posts: 18
jQuery Debian Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BeanUtils is not difficult to use!!!



You can change this so that it encapsulated in a method and you pass the class you need and the hashmap as parameters...
 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

achsu subra wrote:
I am searching how to convert a HashMap to a Javabean. Looking how to create a bean (whose String attributes match with HashMap keys ) given bean class as input along with hashmap.



Do you want to create a class at runtime that has getter / setter that were not known at compile time?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using my own BeanUtil class (link above) it would look like this:
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper de Jong wrote:Did you have a look at Apache BeanUtils or Dozer?

I once wrote some code to view a Java Bean as a Map.



Hi Jesper, i want to know if you show the BeanUtil code
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Gary.

I see the link doesn't work anymore, but I have it here now: BeanAsMap class.
 
He was expelled for perverse baking experiments. This tiny ad is a model student:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic