File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Object to HashMap

 
Samy Ganou
Greenhorn
Posts: 28
  • 0
  • 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
  • 0
  • 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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • 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
  • 0
  • 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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic