Java8 Predicate
code:
package com.qhong; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.qhong.EmployeePredicates.*; public class Main {
public static void main(String[] args) throws Exception {
Employee e1 = new Employee(1,23,"M","Rick","Beethovan");
Employee e2 = new Employee(2,13,"F","Martina","Hengis");
Employee e3 = new Employee(3,43,"M","Ricky","Martin");
Employee e4 = new Employee(4,26,"M","Jon","Lowman");
Employee e5 = new Employee(5,19,"F","Cristine","Maria");
Employee e6 = new Employee(6,15,"M","David","Feezor");
Employee e7 = new Employee(7,68,"F","Melissa","Roy");
Employee e8 = new Employee(8,79,"M","Alex","Gussin");
Employee e9 = new Employee(9,15,"F","Neetu","Singh");
Employee e10 = new Employee(10,45,"M","Naveen","Jain"); List<Employee> employees = new ArrayList<Employee>();
employees.addAll(Arrays.asList(new Employee[]{e1,e2,e3,e4,e5,e6,e7,e8,e9,e10})); System.out.println(filterEmployees(employees, isAdultMale())); System.out.println(filterEmployees(employees, isAdultFemale())); System.out.println(filterEmployees(employees, isAgeMoreThan(35))); //Employees other than above collection of "isAgeMoreThan(35)" can be get using negate()
System.out.println(filterEmployees(employees, isAgeMoreThan(35).negate()));
} } class Employee { public Employee(Integer id, Integer age, String gender, String fName, String lName){
this.id = id;
this.age = age;
this.gender = gender;
this.firstName = fName;
this.lastName = lName;
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public String getGender() {
return gender;
} public void setGender(String gender) {
this.gender = gender;
} public String getFirstName() {
return firstName;
} public void setFirstName(String firstName) {
this.firstName = firstName;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} private Integer id;
private Integer age;
private String gender;
private String firstName;
private String lastName; //Please generate Getter and Setters @Override
public String toString() {
return this.id.toString()+" - "+this.age.toString(); //To change body of generated methods, choose Tools | Templates.
}
}
package com.qhong; import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors; /**
* Created by qhong on 2017/3/14.
*/
public class EmployeePredicates
{
public static Predicate<Employee> isAdultMale() {
return p -> p.getAge() > 21 && p.getGender().equalsIgnoreCase("M");
} public static Predicate<Employee> isAdultFemale() {
return p -> p.getAge() > 18 && p.getGender().equalsIgnoreCase("F");
} public static Predicate<Employee> isAgeMoreThan(Integer age) {
return p -> p.getAge() > age;
} public static List<Employee> filterEmployees (List<Employee> employees, Predicate<Employee> predicate) {
return employees.stream().filter( predicate ).collect(Collectors.<Employee>toList());
}
}
result:
[1 - 23, 3 - 43, 4 - 26, 8 - 79, 10 - 45]
[5 - 19, 7 - 68]
[3 - 43, 7 - 68, 8 - 79, 10 - 45]
[1 - 23, 2 - 13, 4 - 26, 5 - 19, 6 - 15, 9 - 15]
http://howtodoinjava.com/java-8/how-to-use-predicate-in-java-8/
Java8 Predicate的更多相关文章
- 我要告诉你:java接口中可以定义private私有方法
在传统的Java编程中,被广为人知的一个知识点是:java Interface接口中不能定义private私有方法.只允许我们定义public访问权限的方法.抽象方法或静态方法.但是从Java 9 开 ...
- java8 array、list操作 汇【2】)- (Function,Consumer,Predicate,Supplier)应用
static class UserT { String name; public UserT(String zm) { this.name=zm; } public String getName() ...
- Java8 (Function,Consumer,Predicate,Supplier)详解
1. https://blog.csdn.net/lzm18064126848/article/details/70199769 1.1 https://blog.csdn.net/turbo_zon ...
- JAVA8中Predicate,Consumer,UnaryOperator,Function接口的应用
笔者平时时间有限,直接贴代码,关于几个接口的差别,可以查看这两篇文章 感受lambda之美,推荐收藏,需要时查阅 https://juejin.im/post/5ce66801e51d455d850d ...
- Java8常用的内置函数式接口(一)Predicate、Consumer、Supplier、Function
Java8常用的内置函数式接口(一) 简介 JDK 1.8 API中包含了很多内置的函数式接口.有些是在以前版本的Java中大家耳熟能详的,例如Comparator接口,或者Runnable接口.对这 ...
- java集合(3)-Java8新增的Predicate操作集合
Java8起为Collection集合新增了一个removeIf(Predicate filter)方法,该方法将批量删除符合filter条件的所有元素.该方法需要一个Predicate(谓词)对象作 ...
- Java8 函数式接口 @FunctionalInterface以及常用Consumer<T>、Supplier<T>、Function<T, R>、Predicate<T>总结
首先看看什么是Lambda 表达式 Lambda是一个匿名函数,我们可以把Lambda表达式理解为一段可以传递的代码(将代码像数据一样传递):最简单的Lambda表达式可由逗号分隔的参数列表.-> ...
- 探索Java8:(三)Predicate接口的使用
上一篇学习了下Function接口的使用,本篇我们学习下另一个实用的函数式接口Predicate. Predicate的源码跟Function的很像,我们可以对比这两个来分析下.直接上Predicat ...
- java8 函数接口 Predicate例子
import java.util.HashSet; import java.util.Collection; import java.util.function.Predicate; public c ...
随机推荐
- 【linux系列】安装虚拟机时候的3中网络模式
一.桥接 桥接网络是指本地物理网卡和虚拟网卡通过VMnet0虚拟交换机进行桥接,物理网卡和虚拟网卡在拓扑图上处于同等地位,那么物理网卡和虚拟网卡就相当于处于同一个网段,虚拟交换机就相当于一台实现网络中 ...
- Ubuntu adb devices :???????????? no permissions (verify udev rules) 解决方法
Ubuntu adb devices :???????????? no permissions (verify udev rules) 解决方法http://www.cnblogs.com/cat-l ...
- C语言学习链接
C语言博客链接: http://www.cnblogs.com/ningvsban/category/585944.html
- 【BZOJ2141】排队 树状数组+分块
[BZOJ2141]排队 Description 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备 ...
- Linux 搭建Nginx并添加配置 SSL 证书
1. 安装准备 1.1 gcc安装 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: [root@nginx ~]# yum -y i ...
- 一步步从Spring Framework装配掌握SpringBoot自动装配
目录 Spring Framework模式注解 Spring Framework@Enable模块装配 Spring Framework条件装配 SpringBoot 自动装配 本章总结 Spring ...
- Spring.Net的使用
1.Spring.Net的简单介绍 spring.net 框架是微软效仿java中的spring框架而推出的一种在.net中使用的框架,它使用配置的方式实现逻辑的解耦,它的主要功能集成在Spring. ...
- Linux自动化部署尝试
Linux自动化部署尝试 最近做一个项目临近测试,购买的是阿里云的服务器,每次部署都是手动打包war,然后上传到服务器,然后修改配置文件,不仅繁琐,而且费时,就思索着找一个一键式的部署方式,今天终 ...
- 97 条 Linux 常用命令及Vim命令总结
一:Vim编辑模式命令 基本上Vim共分为3种模式,分别是一般模式,编辑模式和命令行模式,这三种模式的作用分别如下简述: 一般模式:默认模式.打开vim直接进入的是一般模式,在这个模式下,可以进行的操 ...
- Spark checkpoint机制简述
本文主要简述spark checkpoint机制,快速把握checkpoint机制的来龙去脉,至于源码可以参考我的下一篇文章. 1.Spark core的checkpoint 1)为什么checkpo ...