对象如下,需求:只要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. 51nod 1657 电子龟

    电子龟的行动,是沿着直线左右走动的.他能够接受两种指令,“T”(向后转,即如果面向左,改成向右:否则就向左)和“F”(向当前面朝的方向往前移动一个单位距离). 现在给出一串指令,让电子龟来执行.你必须 ...

  2. GT源码阅读

    昨天读了一点GT的代码,做个笔记. 参考阅读顺序:https://gt.qq.com/docs/a/UseGtWithBroadcast.txt 在上面的doc上面找到了对应的板块的代码. 1.采集本 ...

  3. python_并发编程——管道

    1.管道 from multiprocessing import Pipe conn1,conn2 = Pipe() #返回两个值 conn1.send('wdc') #发送 print(conn2. ...

  4. Java四种读取和创建XML文档的例子教程

    四种方法解析XML文档:Dom.SAX.JDOM.dom4j          1.了解XML XML,即可扩展标记语言(Extensible Markup Language),标准通用标记语言的子集 ...

  5. nginx添加系统服务(start|stop|restart|reload)

    nginx添加系统服务 1.编写脚本,名为nginx #vim /etc/init.d/nginx #!/bin/bash#chkconfig: - 99 20 #description: Nginx ...

  6. pandas数据类型(二)与numpy的str和object类型之间的区别

    现象: Numpy区分了str和object类型,其中dtype(‘S’)和dtype(‘O’)分别对应于str和object. 然而,pandas缺乏这种区别 str和object类型都对应dtyp ...

  7. Java - 框架之 Spring

    一. IOC 和 DI IOC : 控制反转,将对象的创建权反转给了 Spring.DI  : 依赖注入,前提是必须要有 IOC 的环境,Spring 管理这个类的时候将类的依赖的属性注入(设置)进来 ...

  8. django 第四天模板渲染

    今日内容 一.模板渲染 语法 {{ 变量 }} {% 逻辑 %} 1.变量 取列表中的第几个元素,用索引 <p>{{ namelist.2 }}</p> 取字典中的第几个元素用 ...

  9. 2019/10/22 test T1 题解

    题目描述 给定n个a[i],b[i],求min(x$\in$R){$\sum\limits_{i=1}^{n}$|a[i]*x+b[i]|} 输入格式 第 1行 1个整数 n第 2行 n个整数,第 i ...

  10. 性能测试解读:Kyligence vs Spark SQL

    全球各种大数据技术涌现的今天,为了充分利用大量数据获得竞争优势,企业需要高性能的数据分析平台,可靠并及时地提供对海量数据的分析见解.对于数据驱动型企业,在海量数据上交互式分析的能力是非常重要的能力之一 ...