java stream 流分组过滤

开发中我们经常用到stream来处理数据,我们会遇到对一些数据进行分组过滤

1.场景一 对集合内多个对象单个字段进行分组

@Data
@Accessors(chain = true) //支持链式操作
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class WorkOrder {
    private Integer id;
    private String no;
    private Integer proxyId;
    private String proxyNo;
    private LocalDate planExamDate;
    private Integer totalCount;
    private Integer assignCount;
    private Integer completeCount;
    private Integer qualifiedCount;
    private Integer unqualifiedCount;
}

针对集合内对象某一个字段进行分组

Map> collect = WorkOrders.stream().collect(Collectors.groupingBy(e -> e.getProxyId());

针对集合内对象某多个字段进行分组

Map> collect = WorkOrders.stream().collect(Collectors.groupingBy(e -> e.getProxyId() + "_" + e.getSupplierBatchNo() + "_" + e.getCustomerBatchNo());

针对集合内对象某多个字段进行过滤

Map> collect = WorkOrders.stream().filter(e->e.getId.equals(1)).collect(Collectors.groupingBy(e -> e.getProxyId() + "_" + e.getSupplierBatchNo() + "_" + e.getCustomerBatchNo());

针对集合内对象过滤注意(千万不要写三元运算符)在多条件下可能会失效

你可能感兴趣的:(java,java)