wood burning stoves 2.0*
The moose likes Java in General and the fly likes Map Object to HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Map Object to HashMap" Watch "Map Object to HashMap" New topic
Author

Map Object to HashMap

Samy Ganou
Greenhorn

Joined: Oct 05, 2009
Posts: 28
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

Joined: Nov 27, 2006
Posts: 29
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.

Thanks in advance
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

This won't be a problem as long as you have correctly overriden both the equals and the hashCode methods. What is your problem ?


[My Blog]
All roads lead to JavaRanch
Samy Ganou
Greenhorn

Joined: Oct 05, 2009
Posts: 28
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

Ah, you mean Dozer.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 13, 2007
Posts: 95

I believe Reflection is all you need. Nothing else but the reflection's API from Java.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That's all you *need*, but why reinvent the wheel?
Samy Ganou
Greenhorn

Joined: Oct 05, 2009
Posts: 28
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

Joined: Aug 16, 2005
Posts: 13884
    
  10

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
 
subject: Map Object to HashMap
 
Similar Threads
Difference between Object and Reference, in this Scenario.
storing hashmap in context
using an object in other class...
Object is null
Map map = new Hashmap()