This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Object to HashMap

 
Samy Ganou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object of type ClassA , and I want to map it on a HashMap , Keys of the HashMap will be the attributes name of classA .
Thank you in advance.
 
Joybrata Chakraborty
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samy Ganou wrote:I have an object of type ClassA , and I want to map it on a HashMap , Keys of the HashMap will be the attributes name of classA .
Thank you in advance.


Your question is not clear.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This won't be a problem as long as you have correctly overriden both the equals and the hashCode methods. What is your problem ?
 
Samy Ganou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a solution : the Dozer can help me to do this .. i'm trying to implement ..

My problem by an example :
ObjectA : att1= a1 , att2 = a2 want to transform it to a HashMap containing : {key => "att1", value => a1} etc
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think some of the Apache Bean Utils do this; I'm thinking about the BeanMap type stuff--haven't used it for awhile, though.
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe Reflection is all you need. Nothing else but the reflection's API from Java.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's all you *need*, but why reinvent the wheel?
 
Samy Ganou
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andre Brito wrote:I believe Reflection is all you need. Nothing else but the reflection's API from Java.

Thank you , it's the best solution!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samy Ganou wrote:Thank you , it's the best solution!

Why is it the "best solution"? There's a library that someone else has already built and tested (Dozer), trying to make your own will take you much more time than just using the library.

By the way, I've also once (long ago) written a similar kind of class to view a Java bean as a Map, I have it on my computer somewhere and I've used it in projects on several occasions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic