当前位置: 移动技术网 > IT编程>开发语言>Java > Java lambda list转换map时,把多个参数拼接作为key操作

Java lambda list转换map时,把多个参数拼接作为key操作

2020年08月23日  | 移动技术网IT编程  | 我要评论
我就废话不多说了,大家还是直接看代码吧~map<string, parts> partsmap = synlist.stream().collect(collectors.tomap(k

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

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)); 
}

输出结果

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));
}

输出结果

3. 大坑解决

3.1 法1:value值判空设置

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

studentdtos.stream().collect(collectors.tomap(studentdto::getstudentid, studentdto

-> studentdto.getstudentname()==null?"":studentdto.getstudentname()));

输出结果:

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

3.2 法2:使用collect(supplier<r> supplier, biconsumer<r, ? 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时,把多个参数拼接作为key操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网