通用的java中部分方式实现List<自定义对象>转为List<Map>

自定义类

/**
 * @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.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @date 2023/12/19 11:29
 */
public class ListObjToMapUtils {

    /**
     * List<自定义对象> 转为 List>
     *
     * @param objs
     * @return
     */
    public List> listObjToMap1(List objs) {
        List> results = new ArrayList<>();
        objs.forEach(item -> {
            Map map = null;
            try {
                map = (Map) PropertyUtils.describe(item);
                map.remove("class");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            results.add(map);
        });
        return results;
    }

    /**
     * List<自定义对象> 转为 List>
     *
     * @param objs
     * @return
     */
    public List> listObjToMap2(List objs) {
        List> results = (List>) JSON.parse(JSON.toJSONString(objs));
        return results;
    }

    /**
     * List<自定义对象> 转为 List>
     *
     * @param objs
     * @return
     */
    public List> listObjToMap3(List objs) {
        List> results = new ArrayList<>();
        objs.forEach(item -> {
            Map map = null;
            try {
                map = (Map) BeanUtils.describe(item);
                map.remove("class");
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            results.add(map);
        });
        return results;
    }

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        // Person的修饰符一定要是public
        List personList = new ArrayList<>();
        Person person1 = new Person("小明", "m");
        Person person2 = new Person("小红", "f");
        personList.add(person1);
        personList.add(person2);

        // 使用
        ListObjToMapUtils listObjToMapUtils = new ListObjToMapUtils<>();
        long start1 = System.currentTimeMillis();
        List> results1 = listObjToMapUtils.listObjToMap1(personList);
        long end1 = System.currentTimeMillis();
        System.out.println("PropertyUtils.describe方式耗时 " + (end1 - start1) + " 毫秒");
        long start2 = System.currentTimeMillis();
        List> results2 = listObjToMapUtils.listObjToMap2(personList);
        long end2 = System.currentTimeMillis();
        System.out.println("fastjson方式耗时 " + (end2 - start2) + " 毫秒");
        long start3 = System.currentTimeMillis();
        List> results3 = listObjToMapUtils.listObjToMap3(personList);
        long end3 = System.currentTimeMillis();
        System.out.println("BeanUtils.describe方式耗时 " + (end3 - start3) + " 毫秒");
    }
}

结果

通用的java中部分方式实现List<自定义对象>转为List<Map>_第1张图片

部分maven依赖

        
            org.apache.commons
            commons-lang3
            3.12.0
        
        
            commons-lang
            commons-lang
            2.6
        
        
            com.alibaba
            fastjson
            1.2.41
        
        
            commons-beanutils
            commons-beanutils
            1.9.4
        

你可能感兴趣的:(java,开发语言)