介绍:
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 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作为valueMap 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,元素对象作为valueMap 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 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 collect = personDtos .stream() .collect(Collectors.toMap(new Function() { @Override public Integer apply(PersonDto personDto) { return personDto.getId(); } }, new Function() { @Override public String apply(PersonDto personDto) { return personDto.getName(); } }, new BinaryOperator() { @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重复取第一个nameMap 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 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 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 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 collect = personDtos .stream() .collect(Collectors.toMap(PersonDto::getId, s -> s.getName() == null ? "default" : s.getName())); System.out.println("collect = " + collect);//2使用Optional对值进行包装 Map collect1 = personDtos .stream() .collect(Collectors.toMap(PersonDto::getId,s -> Optional.ofNullable(s.getName()).orElse("default"))); System.out.println("collect1 = " + collect1);//3 使用collect(Supplier supplier, BiConsumer accumulator, BiConsumer combiner) 来构建,可允许null值的出现 HashMap 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值