List<Map<String,Object>> 属性获取

 public static void main(String[] args) {
        // 1.数据准备
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("name", "songwp");
        map.put("age", 25);
        map.put("gender", "male");
        Map<String,Object> map1 = new HashMap<>();
        map1.put("name", "zhangsan");
        map1.put("age", 26);
        map1.put("gender", "male");
        Map<String,Object> map2 = new HashMap<>();
        map2.put("name", "lisi");
        map2.put("age", 24);
        map2.put("gender", "male");
        list.add(map);
        list.add(map1);
        list.add(map2);
        System.out.println("原始数据:"+list);
        // 2.根据name转成Map
        Map<Object, Map<String, Object>> resultMap =
                list.stream().collect(Collectors.toMap(m -> m.get("name"), m -> m));
        System.out.println("根据name转成Map的结果:"+resultMap);
        // 3.根据name转成List
        List<Object> names = list.stream().map(m -> m.get("name")).collect(Collectors.toList());
        System.out.println("根据name转成List的结果;"+names);
    }

 

posted @ 2024-10-28 11:51  [奋斗]  阅读(36)  评论(0编辑  收藏  举报