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

工作记录------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());}}

相关内容

热门资讯

中外对话丨中外专家警告:日本主...   中新网北京12月15日电 题:中外专家警告:日本主动调整军事战略,或走向穷兵黩武  作者 管娜 ...
夏某某(男,大专学历)隐瞒精神... 转自:扬子晚报2024年参军入伍后在安徽出现精神类障碍被退回,2025年隐瞒病史后入伍再被退兵……1...
告别纸上谈兵!AI 培训找哪个...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! (来源:雷达财经)“...
一图读懂vivo S50:田曦...   炒股就看金麒麟分析师研报,权威,专业,及时,全面,助您挖掘潜力主题机会! (来源:快科技)快科...
监管部门出手整治不正当价格行为... 近日,国家市场监督管理总局研究起草了《汽车行业价格行为合规指南(征求意见稿)》(下称《指南》),并向...