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

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...