Java – Convert Object to Map

This tutorial provides several ways of converting a Java object to a Map in Java.

1. Reflection

The traditional way of converting a Java object to a Map is through using the reflection mechanism provided by the JDK.

Suppose we have a class called Student that holds 2 fields id and name. The following method converts the Student object to a Map<String, Object> using reflection:

2. Jackson

The other way of doing the conversion is through using Jackson library.

The following example uses the same example above for converting a Student object to Map<String, Object> using Jackson library.

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar