[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进行了分组,并将分组后,求出了每组中最大值的数据。

相关推荐
咚咚王者29 分钟前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
会编程的林俊杰29 分钟前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
一叶飘零_sweeeet43 分钟前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
朱一头zcy44 分钟前
Win11右键菜单如何把“显示更多选项“中的内容改为默认展示出来
windows
深蓝海拓1 小时前
使matplot显示支持中文和负号
开发语言·python
王家羽翼-王羽1 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java
AntBlack2 小时前
AI Agent : CrewAI 简单使用 + 尝试一下股票分析
后端·python·ai编程
一眼万里*e2 小时前
搭建本地deepseek大模型
python
1***Q7842 小时前
PyTorch图像分割实战,U-Net模型训练与部署
人工智能·pytorch·python
FOREVER-Q2 小时前
Windows 下 Docker Desktop 快速入门与镜像管理
运维·服务器·windows·docker·容器