一段枯  
自认惊叹的桥段,终沦为老生常谈。

有一个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));

 

 

 

posted on 2021-12-29 15:56  唯梦闲人  阅读(809)  评论(0编辑  收藏  举报
 
……