首页 > 编程语言 > 详细

java8 list 转 map

时间:2021-06-01 15:32:43      阅读:19      评论:0      收藏:0      [点我收藏+]

List<NodeRoleRelationListVo> nodeRoleList = nodeRoleRelationFeign.selectList(new NodeRoleRelationReqVo()).getData();
//Stream将List转换为Map,使用Collectors.toMap方法进行转换


//1、指定key-value,value是对象中的某个属性值。
Map<String,String> nodeRoleMap = nodeRoleList.stream().collect(Collectors.toMap(NodeRoleRelationListVo::getNodeCode,NodeRoleRelationListVo::getRoleCode));


//2、指定key-value,value是对象本身,User->User 是一个返回本身的lambda表达式
nodeRoleList.stream().collect(Collectors.toMap(NodeRoleRelationListVo::getNodeCode,NodeRoleRelationListVo->NodeRoleRelationListVo));


//3、指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身
nodeRoleList.stream().collect(Collectors.toMap(NodeRoleRelationListVo::getNodeCode, Function.identity()));


//4、指定key-value,value是对象本身,Function.identity()是简洁写法,也是返回对象本身,key 冲突的解决办法,这里选择第二个key覆盖第一个key。
nodeRoleList.stream().collect(Collectors.toMap(NodeRoleRelationListVo::getNodeCode, Function.identity(),(key1,key2)->key2));

java8 list 转 map

原文:https://www.cnblogs.com/cngsl/p/14836740.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!