使用Java8的Stream流的Collectors.toMap来生成Map结构

问题描述

在日常开发中总会有这样的代码,将一个List转为Map集合,使用其中的某个属性为key,某个属性为value。

常规实现

java 复制代码
public class CollectorsToMapDemo {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Student {
        private String name;
        private Integer age;
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三", 18));
        list.add(new Student("李四", 19));
        list.add(new Student("王五", 20));

        // 将这个list转为map集合,key=age,value=student
        Map<Integer, Student> map = new HashMap<>();
        for (Student student : list) {
            if (map.containsKey(student.getAge())) {
                // 如果key已经存在的解决逻辑
                map.put(student.getAge(), student);
            } else {
                map.put(student.getAge(), student);
            }
        }
    }
}

Java8实现

java 复制代码
public class CollectorsToMapDemo {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Student {
        private String name;
        private Integer age;
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三", 18));
        list.add(new Student("李四", 19));
        list.add(new Student("王五", 20));

        // 将这个list转为map集合,key=age,value=student
        Map<Integer, Student> map = list.stream().collect(Collectors.toMap(Student::getAge, Function.identity()));
        System.out.println(map);
    }
}

输出结果:

解释一下参数:

第一个参数:Student::getAge表示选择Student的getAge作为map的key值;

第二个参数:Function.identity()表示选择将原来的对象作为Map的value值;(也可以使用s -> s来表示选对象)

key值重复,就会报错。

java 复制代码
public class CollectorsToMapDemo {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Student {
        private String name;
        private Integer age;
    }

    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("张三", 18));
        list.add(new Student("李四", 19));
        list.add(new Student("王五", 20));
        list.add(new Student("赵六", 18));

        // 将这个list转为map集合,key=age,value=student
        Map<Integer, Student> map = list.stream().collect(Collectors.toMap(Student::getAge, Function.identity()));
        System.out.println(map);
    }
}

输出结果:

如何解决:

要传第三个参数,就是key值重复的处理逻辑。

例如传(a, b) -> b就表示a和b重复选后输入的b元素。

Map<Integer, Student> map = list.stream().collect(Collectors.toMap(Student::getAge, Function.identity(), (a,b)->b));

相关推荐
营赢盈英18 天前
using showdown js with openAi streaming response
开发语言·前端·javascript·stream·openai api
是枫似风1 个月前
消息驱动Stream---基于SpringCloud
java·spring boot·后端·spring·spring cloud·stream·消息分区
法尼的铁帽子1 个月前
stream流与Predicate结合对集合去重或获取重复元素
java·stream·java8
iFlyCai1 个月前
Flutter中的异步编程
flutter·stream·async·future·await·futurebuilder·flutter异步编程
Hello-Brand1 个月前
Redis系列:使用Stream实现消息队列 (图文总结+Go案例)
redis·stream·高性能·高可用·xadd·xread
伊织code1 个月前
python_rtmpstream - Python rtmp 推流
开发语言·python·ffmpeg·stream·推流·rtmp
逆天-逍遥哥哥2 个月前
JAVA8专题-Stream流操作详解
java·stream·java8
Ramboooooooo2 个月前
Stream Lua Nginx Module 插件一键安装
开发语言·nginx·lua·stream·stream-lua·lua-nginx
罗小爬EX3 个月前
Redis Stream & Redisson Stream
redis·redisson·stream·mq
DJyzh3 个月前
java8 List的Stream流操作 (实用篇 三)
数据结构·list·stream流·stream