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); } }