对象如下,需求:只要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. Git收藏

     Git Flow 的正确使用姿势 https://www.jianshu.com/p/41910dc6ef29 git合并分支上的commit为一条commit到master  https://ww ...

  2. 2.遍历XML即添加修改节点

    1.xml <?xml version="1.0" encoding="utf-8" ?> <stories> <story ac ...

  3. vbs查找Excel中的Sheet2工作表是否存在不存在新建

    set oExcel = CreateObject( "Excel.Application" ) oExcel.Visible = false '4) 打开已存在的工作簿: oEx ...

  4. vsCode与Hubilder中自定义代码块

    平常测试vue的一些属性时总要新增代码,引入文件,麻烦,顾自定义代码块来实现快速新增测试 一.打开设置 1.1 vsCode设置 点击后选择代码块所属的文件类型,比如在html中则选择html类型配置 ...

  5. 行为型模式(四) 观察者模式(Observer)

    一.动机(Motivate) "观察者模式"在现实生活中,实例其实是很多的,比如:八九十年代我们订阅的报纸,我们会定期收到报纸,因为我们订阅了.银行可以给储户发手机短信,也是&qu ...

  6. linux第三天

    一.用户的类型   1.root管理员:所有权限(r w x)   2.文件拥有者(u):谁创建谁拥有   3.组 (g):用户组   4.其它用户(o):不属于用户组,也不是文件的创建者,不是管理员 ...

  7. 【Redis】Linux下Redis的安装

    Redis服务安装 主要有两种方式:apt安装和编译安装. 我采用的是apt安装,系统是ubuntu18.04,Redis version 5:4.0.9-1 在 Ubuntu 系统安装 Redi 可 ...

  8. [golang]golang 汇编

    https://lrita.github.io/2017/12/12/golang-asm/#why 在某些场景下,我们需要进行一些特殊优化,因此我们可能需要用到golang汇编,golang汇编源于 ...

  9. 《挑战30天C++入门极限》类的分解,抽象类与纯虚函数的需要性

        类的分解,抽象类与纯虚函数的需要性 为了不模糊概念在这里我们就简单的阐述一下类的分解,前面的教程我们着重讲述了类的继承,继承的特点就是,派生类继承基类的特性,进行//站点:www.cndev- ...

  10. 《挑战30天C++入门极限》C++类的继承与多重继承的访问控制

        C++类的继承与多重继承的访问控制 在前面的练习中我们一直在使用public的继承方式,即共有继承方式,对于protected和private继承方式,即保护继承与私有继承方式我们并没有讨论. ...