collectingAndThen


有一个List<Map<String,Object>> list, 里面为获取到的流水,由于一条流水需要分开几次操作,现需要对list中map根据流水号去

 
  //得到所有流水号 去重
        List<Object> collect = allList.stream().map(m -> m.get("ca00")).distinct().collect(Collectors.toList());
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        collect.forEach(co -> {//遍历 根据流水号获取流水
            Map<String, Object> map = allList.stream().filter(al -> Objects.equals(al.get("ca00"), co)).findFirst().get();    
            maps.add(map);
        });

  

 

找了一种优雅的写法

list = list.stream()
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                                        () -> new TreeSet<>(Comparator.comparing(da -> da.get("ORDTB407CA00").toString()))),//根据订单号去重
                                ArrayList::new));

  仔细看了一下,发现 collectingAndThen 是先收集结果,然后对收集到的结果做后面 Function 函数的操作

例如下面代码  对收纳的结果进行拼接,然后将拼接结果转大写 

 

 String collect = Stream.of("aoe", "uid", "htn").collect(Collectors.collectingAndThen(Collectors.joining("-"), String::toUpperCase));

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM