• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How to convert HashMap to JavaBean

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22209
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic