对象如下,需求:只要30岁以下的人

//求职者的实体类
public class Person {
private String name;//姓名
private Integer age;//年龄
private String gender;//性别 ...
//省略构造方法和getter、setter方法
... //重写toString,方便观看结果
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
}

1、使用Iterator的传统写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女"));
//过滤30岁以上的求职者
Iterator<Person> iterator = collection.iterator();
while (iterator.hasNext()) {
Person p = iterator.next();
if (p.getAge() >= 30)
iterator.remove();
}
System.out.println(collection.toString());//查看结果

2、不用lambdaremoveIf写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女")); collection.removeIf(new Predicate<Person>() {
@Override
public boolean test(Person p) {
return p.getAge()>=30;//过滤30岁以上的求职者
}
}); System.out.println(collection.toString());//查看结果

3、使用lambdaremoveIf写法(只有一行了,哈哈)

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女")); collection.removeIf(p -> p.getAge() >= 30);//过滤30岁以上的求职者 System.out.println(collection.toString());//查看结果

4、使用lambda的filter写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女")); Stream<Person> personStream = collection.stream().filter(p -> p.getAge() < 30)).collect(Collectors.toList());
//由于使用了stream因此后面需要使用.collect(Collectors.toList())转换成list System.out.println(collection.toString());//查看结果

下面是没有使用lambda的写法

Collection<Person> collection = new ArrayList();
collection.add(new Person("张三", 22, "男"));
collection.add(new Person("李四", 19, "女"));
collection.add(new Person("王五", 34, "男"));
collection.add(new Person("赵六", 30, "男"));
collection.add(new Person("田七", 25, "女")); Stream<Person> personStream = collection.stream().filter(new Predicate<Person>() {
@Override
public boolean test(Person p) {
return p.getAge() < 30;//只保留男性
}
}); collection = personStream.collect(Collectors.toList());//将Stream转化为List
System.out.println(collection.toString());//查看结果

java8 数据集过滤removeIf和filter的更多相关文章

  1. 反向路径过滤——reverse path filter

    原文地址:反向路径过滤——reverse path filter 作者:pwp_cu 反向路径过滤——reverse path filter 一.原理先介绍个非对称路由的概念参考<Underst ...

  2. RxJava【过滤】操作符 filter distinct throttle take skip first MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  3. phalcon: 过滤(Phalcon\Filter())

    过滤,就是清除不需要的数据,留下想要的数据. 其调用方法如下,一: $filter = new \Phalcon\Filter(); $filter->sanitize("some(o ...

  4. XSS过滤JAVA过滤器filter 防止常见SQL注入

    Java项目中XSS过滤器的使用方法. 简单介绍: XSS : 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets, CSS)的缩 ...

  5. django xadmin多对多字段过滤(含filter的反向查询)

    要实现的功能: 继昨天实现拓展User模型使其得到其上级用户,今天要实现某些模型与用户多对多字段过滤功能. 功能描述:以用户指派功能为例,当前用户将文件指派给多个下级,修改前 程序会将所有用户都显示出 ...

  6. 微信小程序 WXS实现json数据需要做过滤转义(filter)

    前言 最近有在做小程序开发,在开发的过程中碰到一点小问题,描述一下先. 本人在职的公司对于后台获取的 json 数据需要做过滤转义的很多,不同的状态码会对应不同的文字,但是在微信小程序中又没有类似 v ...

  7. 数据集 过滤时 RecordCount 属性

    如果是在 OnFilterRecord里写代码,过滤后RecordCount 是不变的. 如果是用 Filter属性过滤,过滤后RecordCount 是变的=过滤后的记录数. 难怪 有的说 变的,有 ...

  8. Django中ORM过滤时objects.filter()无法对月份过滤

    django中的filter日期查询属性有:year.month.day.week_day.hour.minute.second 在做复习博客项目时,我把项目从linux移到了windows,然后博客 ...

  9. 感受一下.net中用 lambda与 linq 做数据集过滤的不同

    lambda: ids.Add( _hahahacontext .hahahamodel .FirstOrDefault( a => //lambda做过滤 a.name == "张宏 ...

随机推荐

  1. java基础(7)---IO流

    一.FileWriter 导包:import java.io.FileWriter 覆盖写入: 追加写入:  写数据换行:  write方法重载: 二.编码: 三.FileReader: read重载 ...

  2. Handling skewed data---Error metrics for skewed(偏斜的) classes(precision&recall)

    skewed classes skewed classes: 一种类里面的数量远远高于(或低于)另一个类,即两个极端的情况. 预测cancer的分类模型,如果在test set上只有1%的分类误差的话 ...

  3. drf框架 - JWT认证插件

    JWT认证 JWT认证方式与其他认证方式对比: 优点 1) 服务器不要存储token,token交给每一个客户端自己存储,服务器压力小 2)服务器存储的是 签发和校验token 两段算法,签发认证的效 ...

  4. Kubernetes 学习17 dashboard认证及分级授权

    一.概述 1.我们前面介绍了kubernetes的两个东西,认证和授权 2.在kubernetes中我们对API server的一次访问大概会包含哪些信息?简单来讲它是restfule风格接口,也就是 ...

  5. Kubernetes 学习4 kubernetes应用快速入门

    一.相关命令 1.kubectl 通过连接api server 进行各k8s对象资源的增删改查,如pod,service,controller(控制器),我们常用的pod控制器replicaset,d ...

  6. 01_搭建新浪云SAE

    Step1:注册新浪云计算平台用新浪微博登陆新浪云计算平台,网址:http://sae.sina.com.cn/ 登陆成功之后会跳转到安全设置页面,安全设置页面要填写的东西比较多,需要注意:安全设置里 ...

  7. 洛谷P5017摆渡车

    题目 一道做法多种多样的题,DP做法的状态也很多. 我用\(dp[i]\)表示在第i秒发车的时间和,然后dp方程就很好写了 \(dp[i] = dp[j] + i车的等待时间\)j属于i-2m ~ i ...

  8. 数据结构实验之排序六:希尔排序 (SDUT 3403)

    其实,感觉好像增量不同的冒泡,希尔排序概念以后补上. #include <bits/stdc++.h> using namespace std; int a[10005]; int b[1 ...

  9. BFC、IFC、FFC、GFC

    FC(Formatting Context) 它是W3C CSS2.1规范中的一个概念,定义的是页面中的一块渲染区域,并且有一套渲染规则,它决定了其子元素将如何定位,以及和其他元素的关系和相互作用. ...

  10. HTML meta pragma no-cache 页面缓存

    HTML meta pragma no-cache 页面缓存不缓存页面(为了提高速度一些浏览器会缓存浏览者浏览过的页面,通过下面的定义,浏览器一般不会缓存页面,而且浏览器无法脱机浏览.) <me ...