[java]集合类stream的相关操作

1.对list中的map进行分组

下面例子中,根据高度height属性进行分组

java 复制代码
List<Map<String, Float>>originalList = new ArrayList<>();
        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",10f);
            put("height", 100.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",11f);
            put("height", 100.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",20f);
            put("height", 200.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",21f);
            put("height", 200.0f);
        }});

        Map<Object, List<Map>> groupedMap = originalList.stream().collect(Collectors.groupingBy(map -> {
            return map.get("height");
        }));

        System.out.println(groupedMap);

执行结果:

从结果可以看到,已经按照height进行分组成功了。

2.对list中的map进行分组,根据分组后的key进行排序

同样,还是先根据height进行分组,默认分组后生成的Map是不会根据高度进行排序的,因为Map没有这个功能,所以需要再多做一次才做。看代码:

java 复制代码
List<Map<String, Float>>originalList = new ArrayList<>();
        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",10f);
            put("height", 100.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",11f);
            put("height", 100.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",20f);
            put("height", 200.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",21f);
            put("height", 200.0f);
        }});

        Map<Object, List<Map>> groupedMap = originalList.stream().collect(Collectors.groupingBy(map -> {
            return map.get("height");
        }));

        // 使用TreeMap对键进行排序
        TreeMap<Object, List<Map>> sortedGroupedMap = new TreeMap<Object, List<Map>>(groupedMap);
        System.out.println(sortedGroupedMap);

打印结果:

可以看到map第一个元素key是100,第二个key是200,说明排序成功

3.对list中的map进行分组,并求每个组中最大值对应的数据

从图中可以看到heigh为100这组里面,有2条数据,这2个也是map,每个map中包含了lng,lat,height,val,现在要将val的值最大的那个map查出来。代码如下:

java 复制代码
 List<Map<String, Float>>originalList = new ArrayList<>();
        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",10f);
            put("height", 100.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",11f);
            put("height", 100.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",20f);
            put("height", 200.0f);
        }});

        originalList.add(new HashMap<String,Float>() {{
            put("lng", 180.0f);
            put("lat",90f);
            put("val",21f);
            put("height", 200.0f);
        }});


        // 按照alti进行分组,并找出每个组中val的最大值
        Map<Object, Optional<Map<String, Float>>> result = originalList.stream()
            .collect(Collectors.groupingBy(map -> map.get("height"),Collectors.maxBy(Comparator.comparing(map -> Float.valueOf(map.get("val"))))
        ));


        System.out.println(result);

运行结果:

从结果可以看出通过height进行了分组,并将分组后,求出了每组中最大值的数据。

相关推荐
2501_9418656310 分钟前
从事件驱动到异步架构的互联网工程语法构建与多语言实践分享
java·开发语言·jvm
算力魔方AIPC3 小时前
从零开始安装Claude Code + GLM 4.7——Windows版
windows
嗯嗯=5 小时前
python学习篇
开发语言·python·学习
WoY20205 小时前
opencv-python在ubuntu系统中缺少依赖
python·opencv·ubuntu
大游小游之老游6 小时前
Python中如何实现一个程序运行时,调用另一文件中的函数
python
全靠bug跑6 小时前
Spring Cache 实战:核心注解详解与缓存过期时间配置
java·redis·springcache
mantch7 小时前
个人 LLM 接口服务项目:一个简洁的 AI 入口
人工智能·python·llm
聆风吟º7 小时前
【数据结构手札】空间复杂度详解:概念 | 习题
java·数据结构·算法
weixin_445054727 小时前
力扣热题51
c++·python·算法·leetcode
计算机程序设计小李同学7 小时前
基于SpringBoot的个性化穿搭推荐及交流平台
java·spring boot·后端