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

java8 多条件的filter过滤的更多相关文章

  1. Java8 使用 stream().filter()过滤List对象(查找符合条件的对象集合)

    内容简介 本文主要说明在Java8及以上版本中,使用stream().filter()来过滤一个List对象,查找符合条件的对象集合. List对象类(StudentInfo) public clas ...

  2. stark组件之delete按钮、filter过滤

    1.构建批量删除按钮 2.filter过滤 3.总结+coding代码 1.构建批量删除按钮 1.admin中每个页面默认都有 2.stark之构建批量删除 3.coding {% extends ' ...

  3. django 模型类的常见字段约束,以及filter 过滤和查询

    null 不设置时默认设置为False.设置为True时,数据库表字段中将存入NULL的记录. null和blank组合使用,null=True,blank=True,表示该字段可以为空 blank ...

  4. Java Filter过滤xss注入非法参数的方法

    http://blog.csdn.NET/feng_an_qi/article/details/45666813 Java Filter过滤xss注入非法参数的方法 web.xml: <filt ...

  5. js--数组的filter()过滤方法的使用

    前言 你还在通过for循环遍历数组吗?你还在遍历之后一项一项的通过if判断过滤你需要的数据吗?你还在写着一大堆代码实现一个简单的过滤数据功能吗?那么,今天他来了.他就是这里要介绍的es6中数组filt ...

  6. Android利用Filter过滤数据

    MainActivity如下: package cc.testfilterable; import java.util.ArrayList; import java.util.HashMap; imp ...

  7. filter过滤action的问题

    今天犯了一个错误,结果白白浪费了半个下午的时间,特记于此. filter过滤Action的时候,要把过滤器配置在Struts2拦截器的前面,这样过滤器才能过滤到Action,否则不可以.

  8. 【原创】Easyui tree filter 过滤本地数据无效的原因

    Easyui tree filter 过滤本地数据无效的解决方式    正确使用方式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  9. angular 如何获取使用filter过滤后的ng-repeat的数据长度

    在做项目的过程中,被产品要求在内容为空的过程中显示提示信息,然哦户内容使用ng-repeat循环输出的,并且使用了filter过滤.后在谷歌上找到解决方案,如下: ​之前代码如下显示: <ul& ...

  10. Java Servlet (1) —— Filter过滤请求与响应

    Java Servlet (1) -- Filter过滤请求与响应 版本: Java EE 6 参考来源: Oracle:The Java EE 6 Tutorial: Filtering Reque ...

随机推荐

  1. Spring Boot Serverless 实战系列 | 性能调优

    ​简介:Spring Boot Serverless 实战系列第四篇来啦,本文将向大家介绍如何对 Serverless 应用进行性能调优. SpringBoot 是基于 Java Spring 框架的 ...

  2. Resin反序列化链分析

    前言 Resin是一个轻量级的.高性能的开源Java应用服务器.它是由Caucho Technology开发的,旨在提供可靠的Web应用程序和服务的运行环境.和Tomcat一样是个服务器,它和hess ...

  3. pnpm的基本原理及快速使用

    基本原理 前置知识:软件链接与硬链接 软链接(符号链接Symbolic link):是一类特殊的文件, 其包含有一条以绝对路径或者相对路径的形式指向其它文件或者目录的引用.在window快捷方式上和其 ...

  4. Ubuntu更新源文件报错:E: 仓库 “http://ppa.launchpad.net/chris-lea/node.js/ubuntu bionic Release” 没有 Release 文件。

    E: 仓库 "http://ppa.launchpad.net/chris-lea/node.js/ubuntu bionic Release" 没有 Release 文件. 一条 ...

  5. python教程1.2:变量+数据类型+运算符

    一.变量 程序是从上到下依次逐⾏执⾏的,所以变量必须先定义,后调⽤, 否则会报错 变量定义规范  二.数据类型 1.数字类型 可⽤ type() ⽅法来查看数据类型  2.字符串 多引号 多引号什么作 ...

  6. Vue3:Cannot read properties of null (reading 'isCE')

    Cannot read properties of null (reading 'isCE')   这个问题是在vue3中引入elementui的列表框时出现的.经过网上查询,有说是装了两个vue版本 ...

  7. k8s master不可以被调度,修改deploy配置让这个可以单独调度上去

    给两个节点添加标签,让pod调度上去,但是kubectl describe pod  发现报错了,因为master不可以被调度,kube002也是设置了污点禁止被调度了 Warning FailedS ...

  8. 【漏洞复现】用友NC-Cloud PMCloudDriveProjectStateServlet接口存在JNDI注入漏洞

    阅读须知 花果山的技术文章仅供参考,此文所提供的信息只为网络安全人员对自己所负责的网站.服务器等(包括但不限于)进行检测或维护参考,未经授权请勿利用文章中的技术资料对任何计算机系统进行入侵操作.利用此 ...

  9. ​一款开源的.NET程序集反编译、编辑和调试神器

    前言 说到.NET相关的反编译工具大家脑海里第一个想到的工具是什么?ILSpy.dnSpy.还是dotPeek?咱们今天的主要内容是讲讲dnSpyEx(dnSpyEx是dnSpy项目的非官方Fork维 ...

  10. 使用 TortoiseGit 时,报 Access denied 错误

    当输入正确的密码时,总是报如下错误: 解决方法: 然后弹出如下对话框: 然后编辑本地配置文件: 然后将红色框的SSH配置改为绿色框的 HTTP配置,点击保存,确定. 然后再进行拉取源码,先输入用户名, ...