java8 多条件的filter过滤
java8 多条件的filter过滤
package com.example.core.mydemo.java; import java.io.Serializable; import java.time.LocalDateTime; public class CostSettleDetailEntity implements Serializable { private static final long serialVersionUID = 1L; /** * id */ private Integer id; /** * 主订单号 */ private String orderNo; /** * 会员号 */ private String memNo; /** * 金额 */ private Integer amt; /** * 费用编码 */ private String sourceCode; /** * 费用来源描述 */ private String sourceDetail; /** * 费用唯一凭证 */ private String uniqueNo; /** * 费用类型 */ private Integer costType; /** * 创建时间 */ private LocalDateTime createTime; /** * 创建人 */ private String createOp; /** * 修改时间 */ private LocalDateTime updateTime; /** * */ private String updateOp; /** * 0-正常,1-已逻辑删除 */ private Integer isDelete; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public String getMemNo() { return memNo; } public void setMemNo(String memNo) { this.memNo = memNo; } public Integer getAmt() { return amt; } public void setAmt(Integer amt) { this.amt = amt; } public String getSourceCode() { return sourceCode; } public void setSourceCode(String sourceCode) { this.sourceCode = sourceCode; } public String getSourceDetail() { return sourceDetail; } public void setSourceDetail(String sourceDetail) { this.sourceDetail = sourceDetail; } public String getUniqueNo() { return uniqueNo; } public void setUniqueNo(String uniqueNo) { this.uniqueNo = uniqueNo; } public Integer getCostType() { return costType; } public void setCostType(Integer costType) { this.costType = costType; } public LocalDateTime getCreateTime() { return createTime; } public void setCreateTime(LocalDateTime createTime) { this.createTime = createTime; } public String getCreateOp() { return createOp; } public void setCreateOp(String createOp) { this.createOp = createOp; } public LocalDateTime getUpdateTime() { return updateTime; } public void setUpdateTime(LocalDateTime updateTime) { this.updateTime = updateTime; } public String getUpdateOp() { return updateOp; } public void setUpdateOp(String updateOp) { this.updateOp = updateOp; } public Integer getIsDelete() { return isDelete; } public void setIsDelete(Integer isDelete) { this.isDelete = isDelete; } } package com.example.core.mydemo.java; import java.util.ArrayList; import java.util.List; /** * filter过滤查询costType = 5 或者 costType=50的费用综合 * output: fineAmt-1 = 399 * fineAmt-2 = 0 * fineAmt-3 = 199 */ public class CostSettleFilterTest { public static void main(String[] args) { List<CostSettleDetailEntity> costSettleDetails = new ArrayList<CostSettleDetailEntity>(); CostSettleDetailEntity entity = new CostSettleDetailEntity(); entity.setOrderNo("3418639"); entity.setMemNo("635206016"); entity.setAmt(99); entity.setSourceCode("4"); entity.setSourceDetail("取消订单违约金"); entity.setCostType(5); costSettleDetails.add(entity); entity = new CostSettleDetailEntity(); entity.setOrderNo("444186390"); entity.setMemNo("635206016"); entity.setAmt(100); entity.setSourceCode("5"); entity.setSourceDetail("取消订单违约金"); entity.setCostType(50); costSettleDetails.add(entity); entity = new CostSettleDetailEntity(); entity.setOrderNo("5699556"); entity.setMemNo("635206016"); entity.setAmt(200); entity.setSourceCode("6"); entity.setSourceDetail("取消订单违约金"); entity.setCostType(6); costSettleDetails.add(entity); //求和 int fineAmt11 =costSettleDetails.stream().mapToInt(CostSettleDetailEntity::getAmt).sum(); System.out.println("fineAmt-1 = " + fineAmt11); //这样写不对,等于是双重过滤了。筛选不了结果 int fineAmt22 =costSettleDetails.stream().filter(obj ->{ return obj.getCostType() != null && 5 == obj.getCostType(); // 5 }).filter(obj ->{ return obj.getCostType() != null && 50 == obj.getCostType(); // 50 }).mapToInt(CostSettleDetailEntity::getAmt).sum(); System.out.println("fineAmt-2 = " + fineAmt22); //正确写法,在filter条件里面写 || 或的条件。 int fineAmt33 =costSettleDetails.stream().filter(obj ->{ return obj.getCostType() != null && ( 5 == obj.getCostType() || 50 == obj.getCostType()); // 5 50 }).mapToInt(CostSettleDetailEntity::getAmt).sum(); System.out.println("fineAmt-3 = " + fineAmt33); } }