Collectors.toMap()的基本使用方法

介绍:

1、Java 8 流的新类 java.util.stream.Collectors 实现了 java.util.stream.Collector 接口,同时又提供了大量的方法对流 ( stream ) 的元素执行 map and reduce 操作,或者统计操作;

2、Collectors.toMap()是Java8引入的流特性,可以把集合转换为Map集合,转换对象中的key不可重复,重复会报错;

3、如果key重复,可以使用合并函数来取其默认值,避免报错。

使用

一、key不重复场景


//基础数据
List<PersonDto> personDtos = ListUtil.toList(
                new PersonDto(1, "张三", "西安", 22),
                new PersonDto(2, "李四", "咸阳", 23),
                new PersonDto(3, "王五", "榆林", 24),
                new PersonDto(4, "赵六", "宝鸡", 25),
                new PersonDto(5, "孙七", "延安", 26)
        );

 


//1.List转Map,id作为key,name作为value
Map<Integer, String> collect = personDtos
                .stream()
                .collect(Collectors.toMap(PersonDto::getId, PersonDto::getName));
System.out.println("collect = " + collect);

结果:	
collect = {1=张三, 2=李四, 3=王五, 4=赵六, 5=孙七}

 


//2. List转Map,id作为key,元素对象作为value
Map<Integer, PersonDto> collect1 = personDtos
                .stream()
                .collect(Collectors.toMap(PersonDto::getId, o -> o));
System.out.println("collect1 = " + collect1);

结果:
collect1 = {1=PersonDto{id=1, name='张三', address='西安', age=22}
          , 2=PersonDto{id=2, name='李四', address='咸阳', age=23}
          , 3=PersonDto{id=3, name='王五', address='榆林', age=24}
          , 4=PersonDto{id=4, name='赵六', address='宝鸡', age=25}
          , 5=PersonDto{id=5, name='孙七', address='延安', age=26}}

二、key重复场景


//基础数据
List<PersonDto> personDtos = ListUtil.toList(
        new PersonDto(1, "张三", "西安", 22),
        new PersonDto(2, "李四1", "咸阳", 23),
        new PersonDto(2, "李四2", "咸阳", 23),
        new PersonDto(3, "王五1", "榆林", 24),
        new PersonDto(3, "王五2", "榆林", 24),
        new PersonDto(4, "赵六1", "宝鸡", 25),
        new PersonDto(4, "赵六2", "宝鸡", 25),
        new PersonDto(5, "孙七", "延安", 26)
);

 


//List转Map,id作为key,name作为value,如果Id重复取第一个name
        Map<Integer, String> collect = personDtos
                .stream()
                .collect(Collectors.toMap(new Function<PersonDto, Integer>() {
                    @Override
                    public Integer apply(PersonDto personDto) {
                        return personDto.getId();
                    }
                }, new Function<PersonDto, String>() {
                    @Override
                    public String apply(PersonDto personDto) {
                        return personDto.getName();
                    }
                }, new BinaryOperator<String>() {
                    @Override
                    public String apply(String s, String s2) {
                        return s;
                    }
                }));
System.out.println("collect = " + collect);
结果:
collect = {1=张三, 2=李四1, 3=王五1, 4=赵六1, 5=孙七}


//List转Map,id作为key,name作为value,如果Id重复取第一个name
Map<Integer, String> collect = personDtos
      .stream()
      .collect(Collectors.toMap(PersonDto::getId, PersonDto::getName, (value1, value2) -> value1));
System.out.println("collect = " + collect);

结果:
collect = {1=张三, 2=李四1, 3=王五1, 4=赵六1, 5=孙七}

 


//List转Map,id作为key,元素对象作为value,如果Id重复取第一个元素
Map<Integer, PersonDto> collect1 = personDtos
      .stream()
      .collect(Collectors.toMap(PersonDto::getId, o -> o, (value1, value2) -> value1));
System.out.println("collect1 = " + collect1);

结果:
collect1 = {1=PersonDto{id=1, name='张三', address='西安', age=22}
          , 2=PersonDto{id=2, name='李四1', address='咸阳', age=23}
          , 3=PersonDto{id=3, name='王五1', address='榆林', age=24}
          , 4=PersonDto{id=4, name='赵六1', address='宝鸡', age=25}
          , 5=PersonDto{id=5, name='孙七', address='延安', age=26}}

 

三、Collectors.toMap()NullPointerException异常处理


public static void main(String[] args) {
   List<PersonDto> personDtos = ListUtil.toList(
        new PersonDto(1, null, "西安", 22),
        new PersonDto(2, "李四", "咸阳", 23),
        new PersonDto(3, "王五", "榆林", 24),
        new PersonDto(4, "赵六", "宝鸡", 25),
        new PersonDto(5, "孙七", "延安", 26)
   );

Map<Integer, String> collect = personDtos
      .stream()
      .collect(Collectors.toMap(PersonDto::getId, s -> s.getName() ));
System.out.println("collect = " + collect);

Exception in thread "main" java.lang.NullPointerException
    at java.util.HashMap.merge(HashMap.java:1225)
    at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
    at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
    at com.cic.dimp.authority.model.SysUserEntity.main(SysUserEntity.java:252)

解决方法


//1设置时增加判断如果是null,则设置默认值

   Map<Integer, String> collect = personDtos
         .stream()
         .collect(Collectors.toMap(PersonDto::getId, s -> s.getName() == null ? "default" : s.getName()));
   System.out.println("collect = " + collect);

//2使用Optional<T>对值进行包装

   Map<Integer, String> collect1 = personDtos
         .stream()
         .collect(Collectors.toMap(PersonDto::getId,s -> Optional.ofNullable(s.getName()).orElse("default")));
   System.out.println("collect1 = " + collect1);

//3 使用collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R> combiner) 来构建,可允许null值的出现

   HashMap<Integer, String> collect2 = personDtos
         .stream()
         .collect(HashMap::new, (n, v) -> n.put(v.getId(), v.getName()), HashMap::putAll);
   System.out.println("collect2 = " + collect2);
}

结果:
collect = {1=default, 2=李四, 3=王五, 4=赵六, 5=孙七}
collect1 = {1=default, 2=李四, 3=王五, 4=赵六, 5=孙七}
collect2 = {1=null, 2=李四, 3=王五, 4=赵六, 5=孙七}

结论


.collect(Collectors.toMap(PersonDto::getId, o -> o, (value1, value2) -> value1));

第一个参数PersonDto::getId 表示选择PersonDto的getId作为map的key值;

第二个参数o -> o表示选择将原来的对象作为map的value值;

第三个参数(value1, value2) -> value1中,如果value1与value2的key值相同,选择value1作为那个key所对应的value值。

 

 

  • 11
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值