list用stream流转map报key重复

我们在利用java8 Lambda 表达式将集合中对象的属性转成Map时就会出现 Duplicate key xxxx , 说白了也就是key 重复了!案例如下:

@Getter

@Setter

@AllArgsConstructor

public class Student{

    private String className;

    private String studentName;


    public static void main(String[] args) {

List<Student> list = new ArrayList<>();

list.add(new Student("一年级二班", "小明"));

list.add(new Student("一年级二班", "小芳"));

list.add(new Student("一年级二班", "小华"));

list.add(new Student("一年级三班", "翠花"));

list.add(new Student("一年级三班", "香兰"));

// 集合中对象属性转map

Map<String, String> map = list.stream().collect(Collectors.toMap(Student :: getClassName, Student :: getStudentName));

System.out.println(map);

}


}

此时将对象的 班级名称为 key 学生名称为 value,但运行时出现了多个相同的key ,此时编译器就会抛出 Duplicate key xxxx

解决方案如下:

我们需要使用toMap的另外一个重载的方法!

Collectors.toMap(keyMapper, valueMapper, mergeFunction)

前两两个参数都是与之前一样 key 和 value得取值属性, 第三个参数是当key 发生重复时处理的方法,注释上的解释如下:

一种合并函数,用于解决两者之间的冲突与提供的相同键相关联的值到{@link Map#merge(Object, Object, BiFunction)}

该合并函数有两个参数,第一个参数为当前重复key 之前对应的值,第二个为当前重复key 现在数据的值。

1、重复时采用后面的value 覆盖前面的value

Map<String, String> map = list.stream().collect(Collectors.toMap(Student :: getClassName, Student :: getStudentName,

(value1, value2 )->{

            return value2;

}));


输出:

{一年级三班=香兰, 一年级二班=小华}

也可以简写成这样:

Map<String, String> map = list.stream().collect(Collectors.toMap(Student :: getClassName, Student :: getStudentName,

(key1 , key2)-> key2 ));

2、重复时将之前的value 和现在的value拼接或相加起来;

Map<String, String> map = list.stream().collect(Collectors.toMap(Student :: getClassName, Student :: getStudentName,

(key1 , key2)-> key1 + "," + key2 ));


输出:

{一年级三班=翠花,香兰, 一年级二班=小明,小芳,小华}

3、将重复key的数据变成一个集合!

java 复制代码
Map<String, List<String>> map = list.stream().collect(Collectors.toMap(Student :: getClassName,

    // 此时的value 为集合,方便重复时操作

    s -> {

List<String> studentNameList = new ArrayList<>();

studentNameList.add(s.getStudentName());

return studentNameList;

    },

    // 重复时将现在的值全部加入到之前的值内

(List<String> value1, List<String> value2) -> {

value1.addAll(value2);

return value1;

    }

));


输出:

{一年级三班=[翠花, 香兰], 一年级二班=[小明, 小芳, 小华]}

总结:

这几个办法都是基于toMap重载方法第三个参数来实现的!至于哪个方法最好,我觉得应该取决于具体业务!

相关推荐
空青72615 分钟前
ChatGPT在Java后端开发中的应用与影响
java·开发语言·人工智能·后端·神经网络·机器学习·chatgpt
莫得等待16 分钟前
MySQL的慢sql
java
威哥爱编程28 分钟前
Nginx性能调优5招35式不可不知的策略实战
java·nginx·性能调优
五月阳光暖洋洋37 分钟前
SpringBoot2.2.6使用spring-boot-validation读取不到自定义配置文件中的属性
java·开发语言·spring boot
刘钢筋universe40 分钟前
leetcode hot100
java·算法·leetcode
java66666888841 分钟前
深入理解Spring Boot中的容器与依赖注入
java·spring boot·后端
u0104058361 小时前
Spring Boot中的依赖注入和控制反转
java·spring boot·后端
猫猫爱吃小鱼粮1 小时前
57、Flink 的项目配置概述
java·flink
龙洋静1 小时前
RabbitMq - Java客户端基础【简单案例 +Work模型】
java·rabbitmq·java-rabbitmq
想要打 Acm 的小周同学呀1 小时前
ThreadLocal学习
android·java·学习