Apart from the trick solution using reflection, you could also try jackson with one line as follows:

objectMapper.convertValue(o, Map.class);

A test case:

    @Test
    public void testConversion() {
        User user = new User();
        System.out.println(MapHelper.convertObject(user));
    }

    @Data
    static class User {
        String name = "Jack";
        boolean male = true;
    }


// output: you can have the right type normally
// {name=Jack, male=true}
Answer from Hearen on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java collections › java map › casting maps to complex objects
Casting Maps to Complex Objects | Baeldung
May 5, 2024 - Manipulating and reconstructing data is often a crucial part of programming in Java. One powerful technique involves casting Map objects into complex objects or POJOs. This can allow us to use our existing data with the benefits of type safety. In this tutorial, we’ll explore three key approaches to converting Map objects into more complex types: Jackson, Gson, and Apache Commons BeanUtils APIs.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Casting Maps into Complex Objects in Java - Java Code Geeks
June 21, 2024 - Jackson is a popular library for processing JSON in Java. It can be used to convert a Map to a POJO using its ObjectMapper class.
🌐
Stack Overflow
stackoverflow.com › questions › 58225191 › how-can-i-convert-a-map-to-an-object
java - How can I convert a map to an object? - Stack Overflow
October 3, 2019 - java · hashmap · apache-commons-beanutils · Share · Improve this question · Follow · asked Oct 3, 2019 at 18:46 · JoBaratti · 111 silver badge11 bronze badge · Add a comment | Sorted by: Reset to default · Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 0 · There's no easy way (without libraries) to convert a Map to object.
🌐
Baeldung
baeldung.com › home › json › jackson › map serialization and deserialization with jackson
Map Serialization and Deserialization with Jackson | Baeldung
January 8, 2024 - The toString() of our Map<MyPair,String> object is: ... There is another option when we deserialize into a Java class that contains a Map; we can use Jackson’s KeyDeserializer class, one of the many Deserialization classes that Jackson offers.
🌐
Coderanch
coderanch.com › t › 425284 › java › convert-hashmap-string-anytype-hashMap
convert hashmap <string, anytype > to hashMap<string, object> (Java in General forum at Coderanch)
To object I want to keep the key as same and want to convert the value as object thanks zuje ... Anything that is not a primitive is already an Object. Therefore, Map<String,Integer> is a Map<? extends String,? extends Object>, and you can use it in the putAll method: Also, most Map implementation classes have a constructor that takes a Map<? extends K,? extends V> so it's even shorter for only one map:
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java collections › java map › converting object to map in java
Converting Object To Map in Java | Baeldung
January 8, 2024 - Apart from serializing the Address object, the code verifies whether the street and city values within the Employee object are consistent with the values stored in the nested map. Gson is an alternative way to utilize the fromJson() method to convert an object to JSON, and then convert the JSON into a HashMap in a subsequent step. The following test uses Gson to convert a Java object (employee) into a map:
🌐
Mkyong
mkyong.com › home › java › java – convert object to map example
Java – Convert Object to Map example | mkyong.com
October 29, 2016 - package com.mkyong.examples; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Arrays; import java.util.Map; public class ObjectToMapExample { public static void main(String[] args) { ObjectMapper oMapper = new ObjectMapper(); Student obj = new Student(); obj.setName("mkyong"); obj.setAge(34); obj.setSkills(Arrays.asList("java","node")); // object -> Map Map<String, Object> map = oMapper.convertValue(obj, Map.class); System.out.println(map); } }
🌐
Cassiomolin
cassiomolin.com › home › archive › converting pojo to map and vice versa with jackson
Converting POJO to Map and vice versa with Jackson · Cássio Molin
June 19, 2026 - Jackson API makes it easy to covert Java objects to a map instance and vice versa. ... // 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.convertValue(map, Foo.class); Copy
🌐
Thetechnojournals
thetechnojournals.com › 2019 › 10 › how-to-convert-java-object-to-map-or.html
How to convert java object to Map or Properties
October 19, 2019 - //Create java object to be converted Employee emp = new Employee(6L, "Emp-1", 30); //Convert to Map<String, String> class Map<String, String> map = convertObjToMap(emp, new TypeReference<Map<String, String>>(){}); System.out.println("Convert to Map<String, String> :"); System.out.println(map); ...
🌐
GitHub
github.com › Jarcionek › Map-To-Object-Converter
GitHub - Jarcionek/Map-To-Object-Converter: A tool that allows to easily convert Map<String, Object> returned by jdbi into staticly typed classes · GitHub
Now all we have to do is to register this method on the converter: public class Employee { public Gender gender; } public class Example { @Test public void convertsMapToEmployee() { Map<String, Object> employeeMap = singletonMap("gender", 1); MapToObjectConverter converter = new MapToObjectConverter(); converter.registerConverter(Gender.class, number -> Gender.fromInt((int) number)); Employee employee = converter.convert(employeeMap, Employee.class); assertEquals(Gender.FEMALE, employee.gender); } }
Author: Jarcionek
🌐
Crunchify
crunchify.com › json tutorials › in java how to convert map / hashmap to jsonobject? [4 different ways]
In Java How to Convert Map / HashMap to JSONObject? [4 Different Ways] • Crunchify
March 19, 2022 - Raw Map ===> {Google=San Jose, Twitter=SFO, Microsoft=Seattle, Facebook=Mountain View, Crunchify=NYC} Method-1: Using Google GSON ==> {"Google":"San Jose","Twitter":"SFO","Microsoft":"Seattle","Facebook":"Mountain View","Crunchify":"NYC"} Pretty JSONObject ==> { "Google": "San Jose", "Twitter": "SFO", "Microsoft": "Seattle", "Facebook": "Mountain View", "Crunchify": "NYC" } Method-2: Using new JSONObject() ==> {"Google":"San Jose","Twitter":"SFO","Microsoft":"Seattle","Facebook":"Mountain View","Crunchify":"NYC"} Method-3: Using ObjectMapper().writeValueAsString() ==> {"Google":"San Jose","Twi
🌐
Baeldung
baeldung.com › home › java › java collections › java map › converting map to map in java
Converting Map to Map in Java | Baeldung
January 8, 2024 - In this tutorial, we’ll discuss different approaches to solving the problem. Also, as we can see, although the type of MAP1 is Map<String, Object>, all entries’ values are strings.
🌐
Coderanch
coderanch.com › t › 525755 › java › convert-HashMap-JavaBean
How to convert HashMap to JavaBean (Java in General forum at Coderanch)
Jesper de Jong wrote:Did you have a look at Apache BeanUtils or Dozer? I once wrote some code to view a Java Bean as a Map. Hi Jesper, Yes, I went through the org.apache.commons.beanutils. But I am not sure how I need use DynaBean or the BeanUtils class. For e.g, I have hashmaps (key = value) as below- attribute[name] = Joan attribute[age] = 30 From this, I need generate the Bean with getter/setter methods for "name" & "age".
🌐
DZone
dzone.com › coding › languages › converting objects to map and back
Converting Objects to Map and Back
June 21, 2019 - Because these objects are almost never maps on their own, what we really need in the conversion is to have a Map where each entry corresponds to a field in the MyObject class. The key in the entry is the name of the field, and the value is the actual value of the field possibly converted to a Map itself.
🌐
Baeldung
baeldung.com › home › java › java collections › java map › convert hashmap to json object in java
Convert Hashmap to JSON Object in Java | Baeldung
January 8, 2024 - In the above code snippet, we created a HashMap object and added key-value pairs. Moreover, we used Jackson’s ObjectMapper to convert the HashMap to a JSON string, and included assertions to verify the success of the conversion. Gson is another popular Java library that we can use to convert a Map to JSON, and vice versa.
🌐
TutorialsPoint
tutorialspoint.com › convert-an-object-to-another-type-using-map-method-with-lambda-in-java
Convert an object to another type using map() method with Lambda in Java?
The map() method is an intermediate operation in a stream object, so we need a terminal method to complete the stream. <R>Stream <R> map(Function<? super T,? extends R> mapper) import java.util.*; import java.util.stream.*; public class EmployeeInformationTest { public static void main(String args[]) { List<Employee> employees = Arrays.asList( new Employee("Jai"), new Employee("Adithya"), new Employee("Raja")); List<String> names = employees.stream() .map(s -> s.getEmployeeName()) // Lambda Expression .collect(Collectors.toList()); System.out.println(names); } } // Employee class class Employe