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的更多相关文章

  1. 云笔记项目- 上传文件报错"java.lang.IllegalStateException: File has been moved - cannot be read again"

    在做文件上传时,当写入上传的文件到文件时,会报错“java.lang.IllegalStateException: File has been moved - cannot be read again ...

  2. eclipse启动报错java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' befo

    报错: java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invo ...

  3. 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 ...

  4. 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 ...

  5. spring mvc处理http请求报错:java.lang.IllegalStateException: getInputStream() has already been called for this request

    发送post请求到controller处理失败,报错日志如下: java.lang.IllegalStateException: getInputStream() has already been c ...

  6. Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]

    1 报错描述 java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @Boot ...

  7. 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) { ...

  8. springboot 启动报错 java.lang.IllegalStateException: Failed to introspect annotated methods on class org

    . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ...

  9. MQ报错java.lang.IllegalStateException: Failed to load ApplicationContext

    这个问题是jdk版本造成的,把jdk1.8换成jdk1.7问题就解决了

随机推荐

  1. 04-String——课后作业1:字串加密

    题目:请编写一个程序,加密或解密用户输入的英文字串要求设计思想.程序流程图.源代码.结果截图. 程序设计思想:首先由用户选择是加密还是解密,利用String类中的charAt函数依次取出字串中的字符, ...

  2. 【新年呈献】高性能网络通信框架 HP-Socket v5.7.1

    项目主页 : http://www.oschina.net/p/hp-socket 开发文档 : https://www.docin.com/p-2287339564.html 下载地址 : http ...

  3. C++ 模板练习1

    //特定的模板友元关系 #include "stdafx.h" #include <iostream> using namespace std; template< ...

  4. mariabd mysql升级mariadb

    还有错误 [root@localhost /]# mysqldump --all-databases --user=root --password --master-data > backupd ...

  5. shell脚本案例

    1.MySQL数据库备份脚本,下面的脚本是Mysql全量备份+异地备份 一般Mysql数据库备份会采用在MYSQL从库上执行全量备份+增量备份方式.在从库备份避免Mysql主库备份的时候锁表造成业务影 ...

  6. hook键盘钩子 带dll

    library Key; uses SysUtils, Classes, HookKey_Unit in 'HookKey_Unit.pas'; {$R *.res} exports HookOn,H ...

  7. 开源PLM软件Aras详解八 Aras之RelationshipTypes关系类详解

    在Aras中,在之前ItemType解析中有提到,Aras中实际ItemType对应的就是一张表,那么,ItemType与ItemType之间是如何关联的呢, 如果我们需要捋清楚ItemType与It ...

  8. Vmware 困惑点记录和解释

    个人理解,如果有不同见解,麻烦请留言,一起进行探讨: DRS和HA是两个独立的功能. 准入控制只是保障有资源打开故障后迁移来的虚拟机,就算自身已经超过切换的阈值了,HA也是可以迁移过去的. 虚拟机允许 ...

  9. css 元素选择器

    子元素选择器 h1 > strong {color:red;} //这个规则会把第一个 h1 下面的两个 strong 元素变为红色,但是第二个 h1 中的 strong 不受影响: <h ...

  10. Python不区别字符串大小写的列表比较法

    一开始想这样写,结果报了索引错误 后来改正: if user_name.lower() in [names.lower() for names in names]: 我觉得应该是in后面应该跟列表,而 ...