Java反射:将List对象集合转为ListMap集合/MapList集合形式(已解决)

今天使用SpringBoot导入common-beanutils运行的时候总是报NoClassDefFoundError: XXX,所以决定自己写一个类实现 将对象List集合转为以下两种形式:
List<T> --> List<Map<String, Object>>, List<T> --> Map<String, List<Object>>


代码

/**
 * 基于反射,将obj转为map
 */
public class BeanUtil {
    
    
    /**
     * Pojo -> Map<String, Object>
     * @param obj
     * @return
     * @throws Exception
     */
    public static Map<String,Object> object2Map(Object obj) throws Exception{
    
    
        Map<String,Object> map = new HashMap<String, Object>();
        Field[] fields = obj.getClass().getDeclaredFields();
        for(Field field:fields){
    
    
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }
        return map;
    }

    /**
     * List<T> --> List<Map<String, Object>>
     * @param objectList
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> List<Map<String, Object>> objectList2ListMap(List<T> objectList) throws Exception {
    
    
        ArrayList<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        for (T t : objectList) {
    
    
            resultList.add(object2Map(t));
        }
        return resultList;
    }

    /**
     * List<T> --> Map<String, List<Object>>
     * @param objectList
     * @param keyName
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T> Map<String, List<Object>> objectList2MapList(List<T> objectList, String[] keyName) throws Exception{
    
    
        Map<String, List<Object>> resultMap = new HashMap<>();
        for(int i = 0; i < keyName.length; i++){
    
    
            List<Object> arrayList = new ArrayList<>();
            for (T t: objectList){
    
    // List有序,所以对每个对象依次变为map,然后得到对应的值,存入arrayList
                arrayList.add(object2Map(t).get(keyName[i]));
            }
            resultMap.put(keyName[i], arrayList);//将keyName和对应List集合存入resultMap
        }
        return resultMap;
    }
}

测试

测试的时候传入的是List<Statistics>类型,以及对应的pojo:

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Statistics {
    
    
    private String className;
    private Double maxScore;
    private Double minScore;
    private Double avgScore;
    private Integer countStudent;// 学生人数

}
@SpringBootTest(classes = ShizuoApplication.class)
@RunWith(SpringRunner.class)
public class StudentTest {
    
    

    @Autowired
    StudentService studentService;
    @Test
    public void test() throws Exception {
    
    
        System.out.println(studentService);
        List<Statistics> statistics = studentService.statistics();
        List<Map<String, Object>> maps = BeanUtil.objectList2ListMap(statistics);
        System.out.println(maps);

        Map<String, List<Object>> map = BeanUtil.objectList2MapList(statistics, new String[]{
    
    "minScore","maxScore","avgScore","countStudent","className"});
        System.out.println(map);
    }
}
indi.huishi.shizuo.service.impl.StudentServiceImpl@8f0007
[{
    
    minScore=76.2, avgScore=83.5333, countStudent=6, className=1, maxScore=88.0}, {
    
    minScore=80.0, avgScore=83.3, countStudent=5, className=2, maxScore=86.0}, {
    
    minScore=59.0, avgScore=72.84, countStudent=5, className=3, maxScore=85.2}, {
    
    minScore=64.0, avgScore=64.0, countStudent=1, className=5, maxScore=64.0}]
{
    
    minScore=[76.2, 80.0, 59.0, 64.0], avgScore=[83.5333, 83.3, 72.84, 64.0], countStudent=[6, 5, 5, 1], className=[1, 2, 3, 5], maxScore=[88.0, 86.0, 85.2, 64.0]}

参考https://www.cnblogs.com/zhuxiaopijingjing/p/12624403.html

猜你喜欢

转载自blog.csdn.net/qq_36937684/article/details/116725780