Java8 stream

  • 排序(倒序,正序去掉reversed(),根据Reporting实体的isDeadLine字段倒序)
   List<Reporting> collect1 = integerListEntry.getValue().stream().sorted(Comparator.comparing(Reporting::getIsDeadLine).reversed()).collect(Collectors.toList());
  • List  转  map 相同key,value 合为一个List(根据ParkingImg实体的PARKING_ID字段排序,如果有多个key是PARKING_ID,结果放入一个集合中)
  Map<String, List<ParkingImg>> collect = parkingImgs.stream().collect(Collectors.toMap(ParkingImg::getPARKING_ID,
                e -> new ArrayList<>(Arrays.asList(e)),
                (List<ParkingImg> oldList, List<ParkingImg> newList) -> {
                    oldList.addAll(newList);
                    return oldList;
                }));
  • 分组(先过滤Discovery实体的unitName字段不为空,然后根据Discovery的unitName字段分组)
   Map<String, List<Discovery>> collect = records.stream().filter(item -> !item.getUnitName().isEmpty()).collect(Collectors.groupingBy(Discovery::getUnitName))
  • 过滤(Discovery实体的feedbackDate字段为空或者findDate字段小于parse(一个时间))
   List<Discovery> collect = discoveryService.list(type, range).stream().filter(item -> item.getFeedbackDate() == null || item.getFindDate().after(parse)).collect(Collectors.toList());      
  • 对集合的每一项都调用一次同一个方法(循环遍历roads集合将Road实体转换为ORoad实体返回ORoad实体集合)。map 方法用于映射每个元素到对应的结果
public static ORoad fill(Road road) {
        ORoad oRoad = new ORoad();
        oRoad.setId(road.getId());
        oRoad.setTargetType("road");
        return oRoad;
    }
    public static List<ORoad> fill(List<Road> roads) {
        return roads.stream().map(ORoad::fill).collect(Collectors.toList());
    }
或者
    public static List<ORoad> fill(List<Road> roads) {
        return roads.stream().map(item -> fill(item)).collect(Collectors.toList());
    }

 

上一篇:JAVA8学习——从使用角度深入Stream流(学习过程)


下一篇:ORACLE批量处理数据之COLLECT BULK INTO 和FORALL的用法