如何在Java中實(shí)現(xiàn)對(duì)象和Map之間的轉(zhuǎn)換
作者:一安 
  本文將介紹幾種不同的方法來實(shí)現(xiàn)Java對(duì)象和Map之間的相互轉(zhuǎn)換,選擇哪種方法取決于項(xiàng)目的具體需求和個(gè)人偏好。
 在Java開發(fā)中,經(jīng)常需要將Java對(duì)象轉(zhuǎn)換成Map,或者反過來將Map轉(zhuǎn)換成Java對(duì)象。這種轉(zhuǎn)換在很多場景下都非常有用,比如在序列化和反序列化過程中、在數(shù)據(jù)傳輸和持久化時(shí)、或者在進(jìn)行對(duì)象屬性的批量操作時(shí)。
本文將介紹幾種不同的方法來實(shí)現(xiàn)Java對(duì)象和Map之間的相互轉(zhuǎn)換,選擇哪種方法取決于項(xiàng)目的具體需求和個(gè)人偏好。
方法一:使用Spring Framework的ReflectionUtils
Bean轉(zhuǎn)為Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公眾號(hào)");
System.out.println(bean2Map(person));
System.out.println(bean2Map2(person));
public static Map<String, Object> bean2Map(Object object) {
    Map<String, Object> map = new HashMap<>();
    ReflectionUtils.doWithFields(object.getClass(), field -> {
        field.setAccessible(true);
        Object value = ReflectionUtils.getField(field, object);
        if (value != null) {
            map.put(field.getName(), value);
        }
    });
    return map;
}
public static Map<String, Object> bean2Map2(Object object) {
    Map<String, Object> map = new HashMap<>();
    Class<?> clazz = object.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        try {
            Object value = field.get(object);
            if (value != null) {
                map.put(field.getName(), value);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Error accessing field: " + field.getName(), e);
        }
    }
    return map;
}Map轉(zhuǎn)為Bean
Map<String, Object> map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公眾號(hào)");
System.out.println(map2Bean(map, Person.class));
System.out.println(map2Bean2(map, Person.class));
public static <T> T map2Bean(Map<String, Object> map, Class<T> clazz) throws IllegalAccessException, InstantiationException {
    T instance = clazz.newInstance();
    ReflectionUtils.doWithFields(clazz, field -> {
        field.setAccessible(true);
        if (map.containsKey(field.getName())) {
            ReflectionUtils.setField(field, instance, map.get(field.getName()));
        }
    });
    return instance;
}
public static <T> T map2Bean2(Map<String, Object> map, Class<T> clazz) throws IllegalAccessException, InstantiationException {
    T instance = clazz.newInstance();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);
        if (map.containsKey(field.getName())) {
            field.set(instance, map.get(field.getName()));
        }
    }
    return instance;
}方法二:使用Hutool工具
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.5</version>
</dependency>Bean轉(zhuǎn)為Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公眾號(hào)");
Map<String, Object> map = BeanUtil.beanToMap(person);Map轉(zhuǎn)為Bean
Map<String, Object> map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公眾號(hào)");
Person person = BeanUtil.toBean(map, Person.class);方法三:使用Jackson工具
Bean轉(zhuǎn)為Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公眾號(hào)");
System.out.println(bean2Map(person));
public static Map<String, Object> bean2Map(Object object) {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.convertValue(object, new TypeReference<Map<String, Object>>() {
        });
    }Map轉(zhuǎn)為Bean
Map<String, Object> map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公眾號(hào)");
System.out.println(map2Bean(map, Person.class));
public static <T> T map2Bean(Map<String, Object> map, Class<T> clazz){
    ObjectMapper objectMapper = new ObjectMapper();
    return objectMapper.convertValue(map, clazz);
}方法四:使用Apache Commons Lang的BeanUtils
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version> 
</dependency>Bean轉(zhuǎn)為Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公眾號(hào)");
System.out.println(bean2Map(person));
public static Map<String, String> bean2Map(Object object) {
    try {
        return BeanUtils.describe(object);
    } catch (Exception e) {
        throw new RuntimeException("Error converting object to map: " + e.getMessage(), e);
    }
}Map轉(zhuǎn)為Bean
Map<String, Object> map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公眾號(hào)");
System.out.println(map2Bean(map, Person.class));
public static <T> T map2Bean(Map<String, ?> map, Class<T> clazz) {
    try {
        T instance = clazz.newInstance();
        BeanUtils.populate(instance, map);
        return instance;
    } catch (Exception e) {
        throw new RuntimeException("Error converting map to object: " + e.getMessage(), e);
    }
}方法五:使用fastjson工具
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.83</version>
</dependency>Bean轉(zhuǎn)為Map
Person person = new Person();
person.setAge(18);
person.setOpenid("123456");
person.setName("一安");
person.setSubName("公眾號(hào)");
System.out.println(JSONObject.parseObject(JSONObject.toJSONString(person)));Map轉(zhuǎn)為Bean
Map<String, Object> map = new HashMap();
map.put("age", 18);
map.put("openid", "123456");
map.put("name", "一安");
map.put("subName", "公眾號(hào)");
System.out.println(JSONObject.parseObject(JSONObject.toJSONString(map), Person.class));責(zé)任編輯:武曉燕 
                    來源:
                    一安未來
 














 
 
 











 
 
 
 