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重载方法第三个参数来实现的!至于哪个方法最好,我觉得应该取决于具体业务!

相关推荐
s:1031 小时前
【框架】参考 Spring Security 安全框架设计出,轻量化高可扩展的身份认证与授权架构
java·开发语言
南山十一少4 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
427724005 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo6 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦6 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个6 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
南宫生9 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长9 小时前
Maven 基础环境搭建与配置(一)
java·maven
风与沙的较量丶10 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_7482517210 小时前
SpringBoot3 升级介绍
java