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

相关推荐
CC.GG13 分钟前
【Linux】进程概念(五)(虚拟地址空间----建立宏观认知)
java·linux·运维
以太浮标1 小时前
华为eNSP模拟器综合实验之- AC+AP无线网络调优与高密场景
java·服务器·华为
Mr__Miss1 小时前
JAVA面试-框架篇
java·spring·面试
小马爱打代码1 小时前
SpringBoot:封装 starter
java·spring boot·后端
STARSpace88881 小时前
SpringBoot 整合个推推送
java·spring boot·后端·消息推送·个推
码农幻想梦1 小时前
实验八 获取请求参数及域对象共享数据
java·开发语言·servlet
a努力。2 小时前
2026 AI 编程终极套装:Claude Code + Codex + Gemini CLI + Antigravity,四位一体实战指南!
java·开发语言·人工智能·分布式·python·面试
Dylan的码园2 小时前
功能包介绍 : calendar
java·jvm·eclipse
二川bro2 小时前
Java集合类框架的基本接口有哪些?
java·开发语言·python
菜鸟233号2 小时前
力扣213 打家劫舍II java实现
java·数据结构·算法·leetcode