工作记录------List转Map的六种方式Stream
创始人
2024-06-03 01:37:39
0

工作记录------List转Map的六种方式Stream

1.key和value都是对象中的某个属性值.
2.key是对象中的某个属性值,value是对象本身(使用返回本身的lambda表达式)
3.key是对象中的某个属性值,value是对象本身(使用Function.identity()的简洁写法
4.key是对象中的某个属性值,value是对象本身,当key冲突时选择第二个key值覆盖第一个key值
5.key是对象中的几个值一起,value是对象本身,当key冲突时选择第二个key值覆盖第一个key值
6.key是几个属性一起,value也是几个属性一起

key和value都是对象中的某个属性值

Map collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto::getWorkCellId));

@Testpublic void testListToMap(){List list = new ArrayList<>();for (int i = 0 ;i<5;i++){PmWorkCellsDto pmAreasT = new PmWorkCellsDto();pmAreasT.setColumIndex(i);pmAreasT.setWorkCellId(i+":value");list.add(pmAreasT);}Map collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto::getWorkCellId));for(Map.Entry entry:collect.entrySet()){System.out.println(entry.getKey()+"--->"+entry.getValue());}}

0—>0:value
1—>1:value
2—>2:value
3—>3:value
4—>4:value

key是对象中的某个属性值,value是对象本身(使用返回本身的lambda表达式)

Map collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto ->PmWorkCellsDto));

@Testpublic void testListToMap(){List list = new ArrayList<>();for (int i = 0 ;i<5;i++){PmWorkCellsDto pmAreasT = new PmWorkCellsDto();pmAreasT.setColumIndex(i);pmAreasT.setWorkCellId(i+":value");list.add(pmAreasT);}Map collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto ->PmWorkCellsDto));for(Map.Entry entry:collect.entrySet()){System.out.println(entry.getKey()+"--->"+entry.getValue());}}

key是对象中的某个属性值,value是对象本身(使用Function.identity()的简洁写法

 @Testpublic void testListToMap(){List list = new ArrayList<>();for (int i = 0 ;i<5;i++){PmWorkCellsDto pmAreasT = new PmWorkCellsDto();pmAreasT.setColumIndex(i);pmAreasT.setWorkCellId(i+":value");list.add(pmAreasT);}Map collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex,  Function.identity()));for(Map.Entry entry:collect.entrySet()){System.out.println(entry.getKey()+"--->"+entry.getValue());}}

key是对象中的某个属性值,value是对象本身,当key冲突时选择第二个key值覆盖第一个key值

Map collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, Function.identity(),(oldValue, newValue) -> newValue));

@Testpublic void testListToMap(){List list = new ArrayList<>();for (int i = 0 ;i<5;i++){PmWorkCellsDto pmAreasT = new PmWorkCellsDto();pmAreasT.setColumIndex(i);pmAreasT.setWorkCellId(i+":value");list.add(pmAreasT);}Map collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex,  Function.identity(),(oldValue, newValue) -> newValue));for(Map.Entry entry:collect.entrySet()){System.out.println(entry.getKey()+"--->"+entry.getValue());}}

key是对象中的几个值一起,value是对象本身,当key冲突时选择第二个key值覆盖第一个key值

Map collect = list.stream().collect(Collectors.toMap(x->{
return x.getColumIndex()+x.getWorkCellId();
}, Function.identity(),(oldValue, newValue) -> newValue));

 @Testpublic void testListToMap(){List list = new ArrayList<>();for (int i = 0 ;i<5;i++){PmWorkCellsDto pmAreasT = new PmWorkCellsDto();pmAreasT.setColumIndex(i);pmAreasT.setWorkCellId(i+":value");list.add(pmAreasT);}Map collect = list.stream().collect(Collectors.toMap(x->{return x.getColumIndex()+x.getWorkCellId();},  Function.identity(),(oldValue, newValue) -> newValue));for(Map.Entry entry:collect.entrySet()){System.out.println(entry.getKey()+"--->"+entry.getValue());}}

同理如果想要key是几个属性一起,value也是几个属性一起

Map collect = list.stream().collect(Collectors.toMap(x->{
return x.getColumIndex()+x.getWorkCellId();
}, y->{
return y.getColumIndex()+y.getWorkCellId();
},(oldValue, newValue) -> newValue));

@Testpublic void testListToMap(){List list = new ArrayList<>();for (int i = 0 ;i<5;i++){PmWorkCellsDto pmAreasT = new PmWorkCellsDto();pmAreasT.setColumIndex(i);pmAreasT.setWorkCellId(i+":value");list.add(pmAreasT);}Map collect = list.stream().collect(Collectors.toMap(x->{return x.getColumIndex()+x.getWorkCellId();},  y->{return y.getColumIndex()+y.getWorkCellId();},(oldValue, newValue) -> newValue));for(Map.Entry entry:collect.entrySet()){System.out.println(entry.getKey()+"--->"+entry.getValue());}}

相关内容

热门资讯

横琴五年产业规划出炉!明确四大... 近日,《横琴粤澳深度合作区产业发展规划(2025-2029年)》(下称《产业规划》)正式印发,指导合...
伤膝伤脚还影响走路!这几类鞋建... 来源:人民网科普 走路多了脚疼、膝盖疼可能是鞋子没选对怎样选择适合自己的鞋子?如何加强小腿和足部肌肉...
出行提醒!济宁526路公交线路... 6月30日,济宁市公共交通集团有限公司发布公告,自2025年7月4日起,对公交526路进行优化调整,...
多名村民称厕改补贴疑遭冒领,官... 安徽凤阳县调查组7月1日发布情况通报:针对我县枣巷镇花园湖村改厕工作中存在的问题,近日,调查组进行了...
工商银行大理分行斩获“全国文明... 转自:新华财经在“苍洱毓秀”的千年古城大理,中国工商银行大理分行(以下简称“大理分行”)以“金融报国...
如何评价《何以为家》这部电影? 如何评价《何以为家》这部电影?看电影的时候对主人公有过换位思考么?昨晚看了这部电影,一直睡不着,快哭...
千亿军团又双叒扩容!浙股半年市... (转自:科技金融时报)编辑 | 杨柳树                校读 | 胡丽华责编 | 江英...
中科环保:3.54亿元收购贵港... 人民财讯7月1日电,中科环保(301175)7月1日晚间公告,公司以公开摘牌方式收购广西贵港北控水务...
西安一高校图书馆罗马仕充电宝自... 7月1日上午,西安医学院未央校区图书馆发生意外事件,一学生所带罗马仕充电宝在书包内发生自燃,冒出浓浓...
成方圆、郭蓉等追忆著名表演艺术... 转自:北京日报客户端“对现在的年轻演员来说,王昆老师是一个历史人物,但对我们这代人来说,王昆老师是我...