使用stream的Collectors.toMap()方法常见问题如何解决(stream,开发技术)

时间:2024-05-06 09:32:37 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

    使用stream的Collectors.toMap()方法常见问题

    java8开始的流式编程很大程度上简化了我们的代码,提高了开发效率。

    我们经常会使用到stream的Collectors.toMap()来将List转换Map

    在使用过程中有两个小坑需要注意

    1、java.lang.IllegalStateException: Duplicate key

    2、java.lang.NullPointerException

    第一个是由于在List转Map过程中Map集合的key重复导致的;

    第二个是由于在List转Map过程中Map集合的value有null导致的(当存在value值为空时,使用Collectors.toMap()会报NPE,因为底层调用了Map的merge方法,而map方法规定了此处的vlue不能为null,从而抛出空指针异常);

    解决方案

    1、Collectors.toMap(dto ->key值 , dto -> dto,(v1,v2) -> v1)

    在后面添加(v1,v2)->v1 指定选取第一个值 当key值重复的时候,根据情况而定选取第一个还是第二个)

    2、自定义一个Map来接收,不使用Collectors.toMap()

    使用stream的Collectors.toMap()方法常见问题如何解决

    第一种情况示例:

    importcom.google.common.collect.Lists;importjava.util.List;importjava.util.Map;importjava.util.stream.Collectors;importlombok.Data;publicclassTest{privatestaticList<User>userList=Lists.newArrayList();@DatapublicstaticclassUser{privateStringuserCode;privateStringuserName;}/***初始化数据*(这里的userCode=10002重复)*/publicstaticvoidinitData(){Useruser1=newUser();user1.setUserCode("10001");user1.setUserName("张三");Useruser2=newUser();user2.setUserCode("10002");user2.setUserName("李四");Useruser3=newUser();user3.setUserCode("10002");user3.setUserName("王五");userList.add(user1);userList.add(user2);userList.add(user3);}publicstaticvoidmain(String[]args){initData();//反例//Map<String,String>userMap=userList.stream().collect(Collectors.toMap(User::getUserCode,User::getUserName));//正例,在后面添加(u1,u2)->u1指定选取第一个值当key值重复的时候,根据情况而定选取第一个还是第二个Map<String,String>userMap=userList.stream().collect(Collectors.toMap(User::getUserCode,User::getUserName,(u1,u2)->u1));System.out.println(userMap);}}

    第二种情况示例:

    importcom.google.common.collect.Lists;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.stream.Collectors;importlombok.Data;publicclassTest{privatestaticList<User>userList=Lists.newArrayList();@DatapublicstaticclassUser{privateStringuserCode;privateStringuserName;}/***初始化数据*(这里的userCode=10003的userName为空)*/publicstaticvoidinitData(){Useruser1=newUser();user1.setUserCode("10001");user1.setUserName("张三");Useruser2=newUser();user2.setUserCode("10002");user2.setUserName("李四");Useruser3=newUser();user3.setUserCode("10003");user3.setUserName(null);userList.add(user1);userList.add(user2);userList.add(user3);}publicstaticvoidmain(String[]args){initData();//反例//Map<String,String>userMap=userList.stream().collect(Collectors.toMap(User::getUserCode,User::getUserName));//正例(如果对转换后的顺序有要求,这里还可以使用LinkedHashMap)Map<String,String>userMap=userList.stream().collect(HashMap::new,(map,user)->map.put(user.getUserCode(),user.getUserName()),HashMap::putAll);System.out.println(userMap);}}

    Stream ToMap(Collectors.toMap) 实践

    Requirements

    List TO Map

    List Stream 转换 Map时向collect()方法中传递Collector对象,对象由Collectors.toMap()方法返回。

    如下实现List转换为Map

    List<GroupBrandCateBO>list=newArrayList<>(Arrays.asList(newGroupBrandCateBO("v1","g1","b1"),newGroupBrandCateBO("v1","g1","b1"),newGroupBrandCateBO("v3","g3","b3")));Map<String,String>map=list.stream().collect(Collectors.toMap(item->item.getVersion(),item->item.getGroupCode(),(oldVal,currVal)->oldVal,LinkedHashMap::new));System.out.println(map.getClass());Map<String,String>map0=list.stream().collect(Collectors.toMap(item->item.getVersion(),item->item.getGroupCode(),(oldVal,currVal)->oldVal));System.out.println(map0.getClass());System.out.println(map0.toString());Map<String,String>map1=list.stream().collect(Collectors.toMap(GroupBrandCateBO::getVersion,GroupBrandCateBO::getGroupCode));System.out.println(map1.toString());

    Console
    class java.util.LinkedHashMap
    class java.util.HashMap
    {v1=g1, v3=g3}
    Exception in thread “main” java.lang.IllegalStateException: Duplicate key g1
    at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
    &hellip;

    问题分析

    toMap()函数重载:

    • 未指定合并函数mergeFunction情况下,传入throwingMerger()返回BinaryOperator对象,当出现key重复时,调用合并函数!

    • 未指定Supplier实例情况下,默认生成HashMap实例。

    publicstatic<T,K,U>Collector<T,?,Map<K,U>>toMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper){returntoMap(keyMapper,valueMapper,throwingMerger(),HashMap::new);}publicstatic<T,K,U>Collector<T,?,Map<K,U>>toMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper,BinaryOperator<U>mergeFunction){returntoMap(keyMapper,valueMapper,mergeFunction,HashMap::new);}publicstatic<T,K,U,MextendsMap<K,U>>Collector<T,?,M>toMap(Function<?superT,?extendsK>keyMapper,Function<?superT,?extendsU>valueMapper,BinaryOperator<U>mergeFunction,Supplier<M>mapSupplier){BiConsumer<M,T>accumulator=(map,element)->map.merge(keyMapper.apply(element),valueMapper.apply(element),mergeFunction);returnnewCollectorImpl<>(mapSupplier,accumulator,mapMerger(mergeFunction),CH_ID);}privatestatic<T>BinaryOperator<T>throwingMerger(){return(u,v)->{thrownewIllegalStateException(String.format("Duplicatekey%s",u));};}

    补充

    关于合并函数

    List<GroupBrandCateBO>list=newArrayList<>(Arrays.asList(newGroupBrandCateBO("v1","g1","b1"),newGroupBrandCateBO("v1","g2","b2"),newGroupBrandCateBO("v1","g2","b2"),newGroupBrandCateBO("v3","g3","b3")));Map<String,String>map00=list.stream().collect(Collectors.toMap(item->item.getVersion(),item->item.getGroupCode(),(oldVal,currVal)->currVal));Map<String,String>map01=list.stream().collect(Collectors.toMap(item->item.getVersion(),item->item.getGroupCode(),(oldVal,currVal)->oldVal+currVal));System.out.println(map00.toString());System.out.println(map01.toString());

    Console
    {v1=g2, v3=g3}
    {v1=g1g2g2, v3=g3}

    传入Lambda表达式将转化为BinaryOperator<U> mergeFunction对象,合并处理value,非Key!!!

    比如:

    (oldVal,currVal)->currVal)//key相同时当前值替换原始值(oldVal,currVal)->oldVal+currVal//key相同时保留原始值和当前值
     </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
    本文:使用stream的Collectors.toMap()方法常见问题如何解决的详细内容,希望对您有所帮助,信息来源于网络。
    上一篇:Golang中关于defer的知识点有哪些下一篇:

    7 人围观 / 0 条评论 ↓快速评论↓

    (必须)

    (必须,保密)

    阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18