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

工作记录------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<Integer, String> collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto::getWorkCellId));

@Test
    public void testListToMap(){
        List<PmWorkCellsDto> 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<Integer, String> collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto::getWorkCellId));
        for(Map.Entry<Integer, String> 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<Integer, PmWorkCellsDto> collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto ->PmWorkCellsDto));

@Test
    public void testListToMap(){
        List<PmWorkCellsDto> 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<Integer, PmWorkCellsDto> collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex, PmWorkCellsDto ->PmWorkCellsDto));
        for(Map.Entry<Integer, PmWorkCellsDto> entry:collect.entrySet()){
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }

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

 @Test
    public void testListToMap(){
        List<PmWorkCellsDto> 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<Integer, PmWorkCellsDto> collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex,  Function.identity()));
        for(Map.Entry<Integer, PmWorkCellsDto> entry:collect.entrySet()){
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }

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

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

@Test
    public void testListToMap(){
        List<PmWorkCellsDto> 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<Integer, PmWorkCellsDto> collect = list.stream().collect(Collectors.toMap(PmWorkCellsDto::getColumIndex,  Function.identity(),(oldValue, newValue) -> newValue));
        for(Map.Entry<Integer, PmWorkCellsDto> entry:collect.entrySet()){
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }

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

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

 @Test
    public void testListToMap(){
        List<PmWorkCellsDto> 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<String, PmWorkCellsDto> collect = list.stream().collect(Collectors.toMap(x->{
            return x.getColumIndex()+x.getWorkCellId();
        },  Function.identity(),(oldValue, newValue) -> newValue));
        for(Map.Entry<String, PmWorkCellsDto> entry:collect.entrySet()){
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }

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

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

@Test
    public void testListToMap(){
        List<PmWorkCellsDto> 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<String, String> collect = list.stream().collect(Collectors.toMap(x->{
            return x.getColumIndex()+x.getWorkCellId();
        },  y->{
            return y.getColumIndex()+y.getWorkCellId();
        },(oldValue, newValue) -> newValue));
        for(Map.Entry<String, String> entry:collect.entrySet()){
            System.out.println(entry.getKey()+"--->"+entry.getValue());
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值