java中部分方式实现自定义对象转为Map

2023-12-20 11:00:18

自定义类

/**
 * @date 2023/12/19 11:20
 */
public class Person {
    private String name;
    private String sex;

    public Person() {
    }

    public Person(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public String getSex() {
        return sex;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

工具方法

import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义对象转为Map
 *
 * @date 2023/12/19 14:17
 */
public class MapToObjUtils {

    /**
     * 自定义对象转为Map
     * <p>
     * PropertyUtils.describe方式
     *
     * @param obj
     * @return
     */
    public static Map<String, Object> objToMap1(Object obj) {
        Map<String, Object> map = null;
        try {
            map = (Map<String, Object>) PropertyUtils.describe(obj);
            map.remove("class");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException | InvocationTargetException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 自定义对象转为Map
     * <p>
     * 反射方式
     *
     * @param object
     * @return
     */
    public static Map<String, Object> objToMap2(Object object) {
        Map<String, Object> dataMap = new HashMap<>();
        Class<?> clazz = object.getClass();
        for (Field field : clazz.getDeclaredFields()) {
            try {
                field.setAccessible(true);
                dataMap.put(field.getName(), field.get(object));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return dataMap;
    }

    /**
     * 自定义对象转为Map
     * <p>
     * fastjson
     *
     * @param object
     * @return
     */
    public static Map<String, Object> objToMap3(Object object) {
        return (Map<String, Object>) JSON.parse(JSON.toJSONString(object));
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        Person person1 = new Person("小明", "m");

        long start1 = System.currentTimeMillis();
        Map<String, Object> results1 = MapToObjUtils.objToMap1(person1);
        long end1 = System.currentTimeMillis();
        System.out.println("自定义对象转为Map PropertyUtils.describe方式耗时 " + (end1 - start1) + " 毫秒");
        long start2 = System.currentTimeMillis();
        Map<String, Object> results2 = MapToObjUtils.objToMap1(person1);
        long end2 = System.currentTimeMillis();
        System.out.println("自定义对象转为Map 反射方式耗时 " + (end2 - start2) + " 毫秒");
        long start3 = System.currentTimeMillis();
        Map<String, Object> results3 = MapToObjUtils.objToMap1(person1);
        long end3 = System.currentTimeMillis();
        System.out.println("自定义对象转为Map fastjson方式耗时 " + (end3 - start3) + " 毫秒");
    }
}

结果

文章来源:https://blog.csdn.net/NN2016160626/article/details/135084657
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。