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

相关推荐
用户8356290780517 分钟前
使用 Python 在 PDF 中创建与管理书签
后端·python
MeixianAgent5 小时前
Python 回测数据入口怎么验?历史 K 线入库前先做 5 个检查
后端·python
SamDeepThinking7 小时前
裁掉那个差程序员后,给你看团队里高手的代码:这个习惯,希望你有
java·后端·程序员
朕瞧着你甚好8 小时前
技术雷达 & Java 集成评估报告 — Apache Tika 3.3.1
java·ai编程
咕白m6258 小时前
用 Python 实现一键批量查找与替换 Excel 数据
后端·python
MacroZheng9 小时前
短短几天,暴涨2.8万Star!又一款编程神器开源!
java·人工智能·后端
SamDeepThinking9 小时前
函数式编程:用BiFunction消除多类型分支的代码重复
java·后端·面试
SelectDB1 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
Flittly1 天前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了1 天前
Java 生成二维码解决方案
java·后端