首页 > 其他分享 >stream流中toMap()api和Duplicate key问题

stream流中toMap()api和Duplicate key问题

时间:2023-02-04 18:34:02浏览次数:35  
标签:Map toMap stream 流中 collect User key

1、指定key-value,value是对象中的某个属性值。

 Map<Integer,String> userMap = userList.stream().collect(Collectors.toMap(User::getId,User::getName));

2、指定key-value,value是对象本身

User->User 是一个返回本身的lambda表达式

Map<Integer,User> userMap = userList.stream().collect(Collectors.toMap(User::getId,User->User));

Function.identity()是简洁写法,也是返回对象本身

Map<Integer,User> userMap = userList.stream().collect(Collectors.toMap(User::getId, Function.identity()));

3、返回其他Map

解决了Key冲突并转为了TreeMap

userList.stream().collect(Collectors.toMap(User::getName, Function.identity(), (oldVal, newVal) -> newVal, TreeMap::new));

4、解决重复key问题

如果生成Map时有重复key(通过key类型的equals方法来判断)就会报错:java.lang.IllegalStateException: Duplicate key。

当发生重复时这里选择第二个key覆盖第一个key,当然如果需要第一个key那就选择oldKey(可提前先进行指定规则的排序)

Map<Integer,User> userMap4 = userList.stream().collect(Collectors.toMap(User::getId, Function.identity(),(oldKey,newKey)->newKey));

PS:当然也可以提前将集合进行去重处理(stream根据指定字段去重)再进行简单的toMap操作。

5、其他示例

  • 重复时将前面的value 和后面的value拼接起来
Map<String, String> map = list.stream().collect(Collectors.toMap(Person::getId, Person::getName,(key1 , key2)-> key1+","+key2 ));
  • value转为其他DTO实体并解决key重复问题
Map<Integer, ProductCategoryResultDTO> resultDTOMap = byCategoryFidAndEnvironmentTag.stream()
                    .collect(Collectors.toMap(ProductCategory::getCategoryId, v -> {
                        ProductCategoryResultDTO productCategoryResultDTO = new ProductCategoryResultDTO();
productCategoryResultDTO.setStatus(v.getUplowState());
                        return productCategoryResultDTO;
                    }, (key1, key2) -> key2));

 

标签:Map,toMap,stream,流中,collect,User,key
From: https://www.cnblogs.com/better-farther-world2099/p/17092111.html

相关文章

  • FileInputStream和FileOutputStream
    FileInputstream字节输入流用于文件内容的读取操作.创建FileInputstream对象用于读取文件内容,使用后需要进行关闭操作常用方法:read();//每次仅读取一个字节,返回......
  • gstreamer 基础知识
    Gstreamer基础知识1.Gstreamer组件创建一个Gstelement办法是借助于GstElementFactory工厂对象。//mad是工厂对象的名称decoder是创建出element的名字。e......
  • Java8新特性 - Lambda表达式 - Stream API - 时间日期 API
    1.Lambda表达式基础语法1.1定义函数式接口packagecom.yq.demo;/***Lambda表达式需要“函数式接口”的支持*函数式接口:接口中只有一个抽象方法的接口,称为函数式......
  • 【计算机网络】Stanford CS144 Lab1 : stitching substrings into a byte stream 学
    Puttingsubstringsinsequence实现一个流重组器。可以将带有索引的流碎片按照顺序重组。这些流碎片是可以重复的部分,但是不会有冲突的部分。这些流碎片将通过Lab0中......
  • stream操作常用API 示例详解
    简介从JDK8开始,增加了一新特性Stream流式操作,Stream中提供了非常多的API供大家使用,灵活的使用这些API,可以非常的方便且优美的实现我们的代码逻辑。最终型toArraytoArray......
  • java8 stream 用法
    1privateList<User>users=Arrays.asList(2newUser("1001","John",16),3newUser("1002","Carly",17),4new......
  • 【Servlet】Response的OutputStream与Writer输出数据乱码的问题
      OutputStream输出中文数据乱码问题解决方式: packagecn.lsh.servlet;importjava.io.IOException;importjava.io.OutputStream;importjavax.servlet.Se......
  • 【Flink】详解StreamGraph
    【Flink】详解StreamGraph大家好,我们的gzh是朝阳三只大明白,满满全是干货,分享近期的学习知识以及个人总结(包括读研和IT),跪求一波关注,希望和大家一起努力、进步!!概述没有看上一......
  • 错误:为 repo 'appstream' 下载元数据失败 : Cannot prepare internal mirrorlist: No
    错误:为repo'appstream'下载元数据失败:Cannotprepareinternalmirrorlist:NoURLsinmirrorlistCentOS8,​​​yum​​​ install失败。CentOSLinux8在2022年1......
  • jdk8 stream文档(开发中常用)
    引言在学习Kotlin语言的过程中,有被Kotlin中函数式编程的语法糖甜到,因此学习了Kotlin“本家”,Java相关的函数式编程知识。在学习Lambda表达式时接触到了Stream,通过阅读Java......