JDK8stream将list转Map对象报错java.lang.IllegalStateException
JDK8有很多新特性,比如lambda表达式,函数式编程以及stream流的使用,这几个新特性,使用过之后就爱不释手了,比如将list集合通过stream可以直接转换成map对象。
语法:
Map map = list.stream.stream().collect(Collectors.toMap(list集合中对象::get属性,list对象别名->list对象别名));
示例:
Map<Integer , EmployeeTeacherCertificate> employeeTeacherCertificateMap =employeeTeacherCertificates.stream().collect(Collectors.toMap(EmployeeTeacherCertificate::getEmployeeId,cert->cert));
说明:
employeeTeacherCertificates List集合对象
EmployeeTeacherCertificate:是List中的集合对象
是不是很简单。
但是,如果list中比如说empId有重复的话,就会报错。如下:
错误信息说,employeeId=4429的值在集合中有重复的。
这个时候怎么解决呢?
我们可以使用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,
(key1 , key2)-> key2 ));
也可以简写成这样:
Map<String, String> map = list.stream().collect(
Collectors.toMap(Student :: getClassName, Student :: getStudentName,
(value1, value2 )->{
return value2;
}));
凯哥这里就使用了第二种方案:
第二种简单也方便看懂。
最后来个总结吧。
总结:
这几个办法都是基于toMap重载方法第三个参数来实现的!至于哪个方法最好,我觉得应该取决于具体业务!
欢迎大家一起学习一起交流。凯哥Java
JDK8stream将list转Map对象报错java.lang.IllegalStateException的更多相关文章
- 云笔记项目- 上传文件报错"java.lang.IllegalStateException: File has been moved - cannot be read again"
在做文件上传时,当写入上传的文件到文件时,会报错“java.lang.IllegalStateException: File has been moved - cannot be read again ...
- eclipse启动报错java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' befo
报错: java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invo ...
- springboot测试启动报错java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
springboot测试启动报错: java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you ne ...
- Spring Scheduled定时任务报错 java.lang.IllegalStateException: Encountered invalid @Scheduled method 'xxx': For input string: "2S"
报错信息如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ding ...
- spring mvc处理http请求报错:java.lang.IllegalStateException: getInputStream() has already been called for this request
发送post请求到controller处理失败,报错日志如下: java.lang.IllegalStateException: getInputStream() has already been c ...
- Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
1 报错描述 java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @Boot ...
- flume-sink报错 java.lang.IllegalStateException: close() called when transaction is OPEN - you must either commit or rollback first
1. 确认代码无误(根据情况修改,表示若获得不了数据不会自动commit或者rollback): Event event = channel.take(); if (event == null) { ...
- springboot 启动报错 java.lang.IllegalStateException: Failed to introspect annotated methods on class org
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ...
- MQ报错java.lang.IllegalStateException: Failed to load ApplicationContext
这个问题是jdk版本造成的,把jdk1.8换成jdk1.7问题就解决了
随机推荐
- 很多内容是转载或copy各个大佬的
很多内容是转载或copy各个大佬的
- Mount error(5):Input/output error on mount
https://superuser.com/questions/850301/mount-error5input-output-error-on-mount When setting up a sha ...
- swift中实现cell中局部播放的动画效果
在cell中 // 播放器动画效果 private var replicatorLayer:ReplicatorLayer = { let layer = ReplicatorLayer.init(f ...
- Short Essay你真的会写了吗?
提到short essay(可能其他essay也一样),很多同学都很头疼.“没有思路?不知从何下笔?没有亮点?”等等,这些都是同学们的致命伤,因此,short essay就成为了广大留学生的“送命题” ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring DI(依赖注入)的实现方式属性注入和构造注入
依赖注入(Dependency Injection,DI)和控制反转含义相同,它们是从两个角度描述的同一个概念. 当某个 Java 实例需要另一个 Java 实例时,传统的方法是由调用者创建被调用者的 ...
- Golang---sort包
Sort 包介绍 Go 语言标准库 sort 包中实现了几种基本的排序算法:插入排序.快速排序和堆排序,但是在使用 sort 包进行排序时无需具体考虑使用哪种排序方式,因为该方法会根据传入的排序的数据 ...
- PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...
- [安洵杯 2019]easy_web
0x00 知识点 md5强类型的绕过 方法比较固定: POST: a=%4d%c9%68%ff%0e%e3%5c%20%95%72%d4%77%7b%72%15%87%d3%6f%a7%b2%1b%d ...
- POJ 1469:COURSES
COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19458 Accepted: 7658 Descript ...
- Python的 5 种高级用法,效率提升没毛病!
任何编程语言的高级特征通常都是通过大量的使用经验才发现的.比如你在编写一个复杂的项目,并在 stackoverflow 上寻找某个问题的答案.然后你突然发现了一个非常优雅的解决方案,它使用了你从不知道 ...