This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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: 21773
103
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!