码迷,mamicode.com
首页 > 其他好文 > 详细

获取List<Map<String, Object>中Map的属性值列表,获取所有map对象的某个属性列表

时间:2021-06-30 17:38:54      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:list   对象属性   osi   code   position   color   objectid   main方法   utils   

获取List<Map<String, Object>中Map的属性值列表,

获取所有map对象的某个属性列表

 

================================

?Copyright 蕃薯耀 2021-06-29

https://www.cnblogs.com/fanshuyao/

 

    /**
     * 获取List列表中的Map对象属性的值
     * @param <T>
     * @param list List<Map<String, Object>> 
     * @param mapValueName Map对象属性名
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> getMapValues(List<Map<String, Object>> list, String mapValueName) throws Exception{
        
        log.info("mapValueName = " + mapValueName);
        
        if(StringUtils.isBlank(mapValueName)) {
            throw new ValidationException("Map对象属性名不能为空");
        }
        
        List<T> objectidList = new ArrayList<T>();
        
        for (Map<String, Object> map : list) {
            objectidList.add((T) map.get(mapValueName));
        }
        return objectidList;
    }

 

Main方法

    public static void main(String[] args) throws Exception {

        List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("name", "aa");
        map1.put("age", 25);
        map1.put("long", 10000000000L);
        map1.put("double", 10000.25d);
        
        
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("name", "bb");
        map2.put("age", 24);
        map2.put("long", 20000000000L);
        map2.put("double", 20000.25d);
        
        Map<String, Object> map3= new HashMap<String, Object>();
        map3.put("name", "cc");
        map3.put("age", 26);
        map3.put("long", 30000000000L);
        map3.put("double", 30000.25d);
        
        list.add(map1);
        list.add(map2);
        list.add(map3);
        
        List<String> listResult = getMapValues(list, "name");
        System.out.println(listResult);
        
        List<Integer> listAge = getMapValues(list, "age");
        System.out.println(listAge);
        
        List<Long> listLong = getMapValues(list, "long");
        System.out.println(listLong);
        
        List<Long> listDouble = getMapValues(list, "double");
        System.out.println(listDouble);
    }

 

输出结果:

mapValueName = name
[aa, bb, cc]

mapValueName = age
[25, 24, 26]

mapValueName = long
[10000000000, 20000000000, 30000000000]

mapValueName = double
[10000.25, 20000.25, 30000.25]

 

 

================================

?Copyright 蕃薯耀 2021-06-29

https://www.cnblogs.com/fanshuyao/

 

获取List<Map<String, Object>中Map的属性值列表,获取所有map对象的某个属性列表

标签:list   对象属性   osi   code   position   color   objectid   main方法   utils   

原文地址:https://www.cnblogs.com/fanshuyao/p/14950990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!