Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, Convert instance of Class to HashMap

Tags:

java

If I have class like this:

class MyObject {
    public int myInt;
    public String myString;
}

Is it possible to convert instance of this class to HashMap without implementing converting code?

MyObject obj = new MyObject();
obj.myInt = 1; obj.myString = "string";
HashMap<String, Object> hs = convert(obj);

hs.getInt("myInt"); // returns 1
hs.getString("myString"); // returns "string"

Does Java provide that kind of solution, or I need to implement convert by myself?

My Class has more than 50 fields and writing converter for each field is not so good idea.

like image 949
Mr.D Avatar asked May 04 '16 09:05

Mr.D


People also ask

Can we convert object to HashMap in Java?

In Java, you can use the Jackson library to convert a Java object into a Map easily.

Can we convert object to Map?

To convert an object to a Map , call the Object. entries() method to get an array of key-value pairs and pass the result to the Map() constructor, e.g. const map = new Map(Object. entries(obj)) . The new Map will contain all of the object's key-value pairs.

How do you turn a POJO into a Map?

A quick look at how to convert a POJO from/to a Map<K, V> with Jackson: // Create ObjectMapper instance ObjectMapper mapper = new ObjectMapper(); // Converting POJO to Map Map<String, Object> map = mapper. convertValue(foo, new TypeReference<Map<String, Object>>() {}); // Convert Map to POJO Foo anotherFoo = mapper.


3 Answers

With jackson library this is also possible

MyObject obj = new MyObject();
obj.myInt = 1;
obj.myString = "1";
ObjectMapper mapObject = new ObjectMapper();
Map < String, Object > mapObj = mapObject.convertValue(obj, Map.class);
like image 62
profcalculus Avatar answered Oct 18 '22 21:10

profcalculus


You can use reflection for implementing this behavior. You can get all fields of the class you want to convert to map iterate over this fields and take the name of each field as key of the map. This will result in a map from String to object.

Map<String, Object> myObjectAsDict = new HashMap<>();    
Field[] allFields = SomeClass.class.getDeclaredFields();
    for (Field field : allFields) {
        Class<?> targetType = field.getType();
        Object objectValue = targetType.newInstance();
        Object value = field.get(objectValue);
        myObjectAsDict.put(field.getName(), value);
    }
}
like image 36
PKuhn Avatar answered Oct 18 '22 21:10

PKuhn


Something like that will do the trick:

MyObject obj = new MyObject();
obj.myInt = 1; obj.myString = "string";
Map<String, Object> map = new HashMap<>();
// Use MyObject.class.getFields() instead of getDeclaredFields()
// If you are interested in public fields only
for (Field field : MyObject.class.getDeclaredFields()) {
    // Skip this if you intend to access to public fields only
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
    map.put(field.getName(), field.get(obj));
}
System.out.println(map);

Output:

{myString=string, myInt=1}
like image 30
Nicolas Filotto Avatar answered Oct 18 '22 21:10

Nicolas Filotto