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

发布时间:2023年12月19日

自定义类

/**
 * @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<T> {

    /**
     * List<自定义对象> 转为 List<Map<String, Object>>
     *
     * @param objs
     * @return
     */
    public List<Map<String, Object>> listObjToMap1(List<T> objs) {
        List<Map<String, Object>> results = new ArrayList<>();
        objs.forEach(item -> {
            Map<String, Object> map = null;
            try {
                map = (Map<String, Object>) 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<Map<String, Object>>
     *
     * @param objs
     * @return
     */
    public List<Map<String, Object>> listObjToMap2(List<T> objs) {
        List<Map<String, Object>> results = (List<Map<String, Object>>) JSON.parse(JSON.toJSONString(objs));
        return results;
    }

    /**
     * List<自定义对象> 转为 List<Map<String, String>>
     *
     * @param objs
     * @return
     */
    public List<Map<String, String>> listObjToMap3(List<T> objs) {
        List<Map<String, String>> results = new ArrayList<>();
        objs.forEach(item -> {
            Map<String, String> map = null;
            try {
                map = (Map<String, String>) 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<Person> personList = new ArrayList<>();
        Person person1 = new Person("小明", "m");
        Person person2 = new Person("小红", "f");
        personList.add(person1);
        personList.add(person2);

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

结果

部分maven依赖

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.41</version>
        </dependency>
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.4</version>
        </dependency>

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