Convert an Object to a Map in Java

In this post we will provide an example on how to convert an object to a map in Java.

Setup:

You will need to include the following dependency

Convert an Object to a Map in Java:

The example below uses a User object which has the following properties

  • Name
  • Age
  • Address (which is another object consisting of variables line1 and postcode)

Using the ObjectMapper class will convert the User object to a Map.

Running the above code produces the following output:

 

Similar posts:

  1. Convert a map to JSON
  2. How to sort a Map
  3. Convert Object to JSON
  4. Convert list to JSON array

References:

  1. Jackson API documentation

Leave a Reply