​

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. 量化投资_Multicharts数组操作函数_zeros()设定数组元素为0(自定义)

    1. 函数的用法类似于Python的zeros函数,给定数组尺寸,让数组的元素归零 //zeros:根据设定的尺寸长度,让一维数组的元素全部归零 inputs: arr[MaxSize]( numer ...

  2. qt 字符串 转换 hex

    1. qt 中两个字符的字符串直接转换为 hex,类似于 "1A" 要转换成 16进制的 0x1A,使用 int QString::toInt(bool *ok, int base ...

  3. maven的理解和使用

    一.maven是什么? maven是项目管理工具 二.maven为什么要用? 在做开发的时候常常会用到外部的工具包(jar包),这就需要你一个一个的去他们的官网下工具包,然后在项目里依赖他们,比较的麻 ...

  4. 自定义alert

    参考:https://www.cnblogs.com/st-leslie/articles/5279864.html 把window.alert=function(){}指向新的方法,即相当于重写 w ...

  5. idea~创建maven webapp项目

    1.选择 org.apache.maven.archtypes:maven-archtype-webapp 2.禁止远程下载 archetypeCatalog=internal 目的是不远程下载,否则 ...

  6. markdown使用介绍

    一.标题,前面加#,加一个 一级标题,两个二级标题,以此类推. 一级标题 二级标题 三级标题

  7. 【数据结构】单链表介绍及leetcode206题反转单链表python实现

    题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...

  8. 吴裕雄--天生自然MySQL学习笔记:MySQL UNION 操作符

    MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中.多个 SELECT 语句会删除重复的数据. 语法 MySQL UNION 操作符语法格式: SELECT ...

  9. HttpClient4.x 上传文件

    https://blog.csdn.net/wsdtq123/article/details/78888734

  10. This inspection highlights chained comparisons that can be simplified.

    https://stackoverflow.com/questions/26502775/pycharm-simplify-chained-comparison In Python you can & ...