温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

利用Java将lambda list转换map并实现拼接参数

发布时间:2020-11-07 16:44:14 来源:亿速云 阅读:908 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关利用Java将lambda list转换map并实现拼接参数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

我就废话不多说了,大家还是直接看代码吧~

Map<String, Parts> partsMap = synList.stream().collect(Collectors.toMap(k ->

k.getOe()+k.getOeId()+k.getPartGroupId()+k.getStdPartId()+k.getBrandCode(), part -> part));

补充知识:Java8 Collectors.toMap的两个大坑

Collectors.toMap()方法的正常使用示例

List<StudentDTO> studentDTOS = Lists.newArrayList();
studentDTOS.add(new StudentDTO(1,"xixi"));
studentDTOS.add(new StudentDTO(2,"houhou"));
studentDTOS.add(new StudentDTO(3,"maomi"));
Map<Integer, String> collect = studentDTOS.stream().collect(
 Collectors.toMap(StudentDTO::getStudentId, StudentDTO::getStudentName));
System.out.println(JSON.toJSON(collect)); // {"1":"xixi","2":"houhou","3":"maomi"}

一. 坑1:Duplicate Key时抛出IllegalStateException异常

1. 概述

按照常规Java的Map思维,往一个map里put一个已经存在的key,会把原有的key对应的value值覆盖。

但Java8中的Collectors.toMap()却不是这样。当key重复时,该方法默认会抛出IllegalStateException异常。

2. 大坑复现

public void streamToMap1() {
 List<StudentDTO> studentDTOS = Lists.newArrayList();
 studentDTOS.add(new StudentDTO(1,"xixi"));
 studentDTOS.add(new StudentDTO(1,"houhou"));
 studentDTOS.add(new StudentDTO(3,"maomi"));
 Map<Integer, String> collect = studentDTOS.stream()
  .collect(Collectors.toMap(StudentDTO::getStudentId, StudentDTO::getStudentName));
 System.out.println(JSON.toJSON(collect)); 
}

输出结果

利用Java将lambda list转换map并实现拼接参数

3. 大坑解决

法1:将toMap方法修改成如下形式,这样就可以使用新的value覆盖原有value。

studentDTOS.stream().collect(Collectors.toMap(StudentDTO::getStudentId,

StudentDTO::getStudentName,(oldValue, newValue) -> newValue));

输出结果:{"1":"houhou","3":"maomi"}

法2:如果需要保留同一个key下所有的值,则可以对value做简单的拼接,如下:

studentDTOS.stream().collect(Collectors.toMap(StudentDTO::getStudentId,

StudentDTO::getStudentName,(oldValue, newValue) -> oldValue + "," + newValue));

输出结果:

{"1":"xixi,houhou","3":"maomi"}

二. 坑2:value为空时抛出NullPointerException异常

1. 概述

当要转化的map的value值中包含空指针时, 会抛出NullPointerException异常。

2. 大坑复现

public void streamToMap2() {
 List<StudentDTO> studentDTOS = Lists.newArrayList();
 studentDTOS.add(new StudentDTO(1,"xixi"));
 studentDTOS.add(new StudentDTO(2,"houhou"));
 studentDTOS.add(new StudentDTO(3,null));
 Map<Integer, String> collect = studentDTOS.stream().collect(Collectors
 .toMap(StudentDTO::getStudentId, StudentDTO::getStudentName));
 System.out.println(JSON.toJSON(collect));
}

输出结果

利用Java将lambda list转换map并实现拼接参数

3. 大坑解决

3.1 法1:value值判空设置

说明:如果是null,则设置成一个特定值。

studentDTOS.stream().collect(Collectors.toMap(StudentDTO::getStudentId, studentDTO

-> studentDTO.getStudentName()==null&#63;"":studentDTO.getStudentName()));

输出结果:

{"1":"xixi","2":"houhou","3":""}

3.2 法2:使用collect(Supplier<R> supplier, BiConsumer<R, &#63; super T> accumulator, BiConsumer<R, R> combiner)方法构建

说明:该方法允许null值。

Map<Integer, String> collect = studentDTOS.stream().collect(HashMap::new, 
 (n, v) -> n.put(v.getStudentId(), v.getStudentName()), HashMap::putAll);
for(Map.Entry<Integer, String> entry:collect.entrySet()){
 System.out.println(entry.getKey()+"="+entry.getValue());
}

输出结果

1=xixi
2=houhou
3=null

3.3 使用Optional对值进行包装

Map<Integer, Optional<String>> collect = studentDTOS.stream().collect(Collectors
 .toMap(StudentDTO::getStudentId,
 studentDTO -> Optional.ofNullable(studentDTO.getStudentName())));
 
for(Map.Entry<Integer, Optional<String>> entry:collect.entrySet()){
 System.out.println(entry.getKey()+"="+entry.getValue().orElse(""));
}

输出结果

1=xixi
2=houhou
3=

以上就是利用Java将lambda list转换map并实现拼接参数,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI