Lambda表达式:可以方便我们把方法当做参数传递

package airycode_java8.nice1;

import org.junit.Test;

import java.util.*;

/**
* Created by admin on 2018/12/28.
*/
public class TestLambda { public static void main(String[] args) {
List<Employee> employees = filterEmployee(employeeList, new FilterEmployeeByAge());
System.out.println(employees);
System.out.println("-------------------------------------------");
List<Employee> employees2 = filterEmployee(employeeList, new FilterEmployeeBySalary());
System.out.println(employees2); System.out.println("=======================");
test1111();
} //匿名内部类
public void test(){
Comparator<Integer> com = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
}; TreeSet<Integer> ts = new TreeSet<>(com);
} //Lambda表达式
public void testL(){
Comparator<Integer> com = (x,y)->Integer.compare(x,y);
TreeSet<Integer> ts = new TreeSet<>(com);
} //准备数据
static List<Employee> employeeList = Arrays.asList(
new Employee("张三",18,9999.99, Employee.Status.FREE),
new Employee("李四",38,5555.55,Employee.Status.BUSY),
new Employee("王五",50,6666.66,Employee.Status.VOCATION),
new Employee("赵六",16,3333.33,Employee.Status.FREE),
new Employee("田七",8,7777.77,Employee.Status.BUSY)
);
//需求:获取当前公司员工年龄大于35的员工的信息
public List<Employee> filterEmployees(List<Employee>employeeList){
List<Employee> emps = new ArrayList<>(); for (Employee emp:employeeList) {
if (emp.getAge() >= 35) {
emps.add(emp);
}
} return emps;
} //需求:改变1:获取当前公司员工工资大于5000的员工信息
public List<Employee> filterEmployees2(List<Employee>employeeList){
List<Employee> emps = new ArrayList<>(); for (Employee emp:employeeList) {
if (emp.getSalary()>=5000) {
emps.add(emp);
}
} return emps;
} //优化方式1:设计模式优(策略设计模式)化上述需求的改变
public static List<Employee> filterEmployee(List<Employee>employeeList,MyPredicate<Employee> mp){
List<Employee> emps = new ArrayList<>(); for (Employee emp:employeeList) {
if (mp.test(emp)) {
emps.add(emp);
}
} return emps;
} //优化方式2:匿名内部类
public static void test1111(){
List<Employee> list = filterEmployee(employeeList, new MyPredicate<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() <= 5000;
}
});
System.out.println(list);
} //优化方式2:匿名内部类
@Test
public void test5(){
List<Employee> list = filterEmployee(employeeList, new MyPredicate<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getSalary() <= 5000;
}
});
System.out.println(list);
} //优化方式3.Lambda表达式
@Test
public void test6(){
List<Employee> employees = filterEmployee(employeeList, employee -> employee.getSalary() <= 5000);
employees.forEach(System.out::println);
} //优化方式4.上述代码不存在的写法(Stream API)
@Test
public void test7(){
employeeList.stream().filter(employee -> employee.getSalary()<5000).forEach(System.out::println);
System.out.println("----------------------------");
//提取所有的名字
employeeList.stream().map(Employee::getName).forEach(System.out::println);
}
} 新建employee类: package airycode_java8.nice1; /**
* Created by admin on 2018/12/28.
*/
public class Employee { private String name;
private int age;
private double salary; private Status status; public Employee() {
super();
} public Employee(int age){
this.age = age;
} public Employee(String name, int age, double salary, Status status) {
this.name = name;
this.age = age;
this.salary = salary;
this.status = status;
} public Status getStatus() {
return status;
} public void setStatus(Status status) {
this.status = status;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public double getSalary() {
return salary;
} public void setSalary(double salary) {
this.salary = salary;
} @Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", status=" + status +
'}';
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; Employee employee = (Employee) o; if (age != employee.age) return false;
if (Double.compare(employee.salary, salary) != 0) return false;
return name != null ? name.equals(employee.name) : employee.name == null;
} @Override
public int hashCode() {
int result;
long temp;
result = name != null ? name.hashCode() : 0;
result = 31 * result + age;
temp = Double.doubleToLongBits(salary);
result = 31 * result + (int) (temp ^ (temp >>> 32));
return result;
} public enum Status{
FREE,BUSY,VOCATION;
}
} package airycode_java8.nice1; /**
* Created by admin on 2018/12/28.
*/
public class FilterEmployeeByAge implements MyPredicate<Employee> { @Override
public boolean test(Employee employee) {
return employee.getAge()>=35;
}
} package airycode_java8.nice1; /**
* Created by admin on 2018/12/28.
*/
public class FilterEmployeeBySalary implements MyPredicate<Employee> { @Override
public boolean test(Employee employee) {
return employee.getSalary()>=5000;
}
} package airycode_java8.nice1; /**
* Created by admin on 2018/12/28.
*/
public interface MyPredicate<T> { public boolean test(T t); }

  

Lambda引言的更多相关文章

  1. 委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式

    引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到和委托相关的匿名方法.Lambda表达式及泛型委托记录 ...

  2. 泛型委托及委托中所涉及到匿名方法、Lambda表达式

    泛型委托及委托中所涉及到匿名方法.Lambda表达式 引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到 ...

  3. 【原创】从策略模式闲扯到lambda表达式

    引言 策略模式,讲这个模式的文章很多,但都缺乏一个循序渐进的过程.讲lambda表达式的文章也很多,但基本都是堆砌一堆的概念,很少带有自己的见解.博主一时兴起,想写一篇这二者的文章.需要说明的是,在看 ...

  4. 十分钟学会Java8:lambda表达式和Stream API

    Java8 的新特性:Lambda表达式.强大的 Stream API.全新时间日期 API.ConcurrentHashMap.MetaSpace.总得来说,Java8 的新特性使 Java 的运行 ...

  5. 怒学Java8系列一:Lambda表达式

    PDF文档已上传Github  Github:https://github.com/zwjlpeng/Angrily_Learn_Java_8 第一章 Lambda 1.1 引言 课本上说编程有两种模 ...

  6. c#封装DBHelper类 c# 图片加水印 (摘)C#生成随机数的三种方法 使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象 c# 制作正方形图片 JavaScript 事件循环及异步原理(完全指北)

    c#封装DBHelper类   public enum EffentNextType { /// <summary> /// 对其他语句无任何影响 /// </summary> ...

  7. SqlHelper简单实现(通过Expression和反射)1.引言

    之前老大说要改变代码中充斥着各种Select的Sql语句字符串的情况,让我尝试着做一个简单的SqlHelper,要具有以下功能: 1.不要在业务代码中暴露DataTable或者DataSet类型: 2 ...

  8. C#进阶之全面解析Lambda表达式

    引言 在实际的项目中遇到一个问题,我们经常在网上搜索复制粘贴,其中有些代码看着非常的简洁,比如Lambda表达式,但是一直没有去深入了解它的由来,以及具体的使用方法,所以在使用的时候比较模糊,其次,编 ...

  9. 感受lambda之美,推荐收藏,需要时查阅

    一.引言二.java重要的函数式接口1.什么是函数式接口1.1 java8自带的常用函数式接口.1.2 惰性求值与及早求值2.常用的流2.1 collect(Collectors.toList())2 ...

随机推荐

  1. 单调性 [1 + 1 / (n)]^n

    def f(n): n += 0.0 s = 1 + 1 / (n) r = pow(s, n) print(n, ',', r) return r l = []for i in range(1, 1 ...

  2. [development][libconfig] 配置文件库

    以前,一直用ini的配置文件. 简单清晰但是不灵活. 换一个: 试试libconfig 主页:  http://www.hyperrealm.com/oss_libconfig.shtml githu ...

  3. 《Mysql DML语句》

    1:DISTINCT 用于去重,但是需要注意的是,它是用于所有列的,也就是说,除非指定的列全部相同,否则所有的行都会被检索出来. 2:ORDER BY 用于排序,但是应该注意的是,它因该是 SELEC ...

  4. 洛谷P3245 大数 [HNOI2016] 莫队

    正解:莫队 解题报告: 传送门 这题首先要发现一个结论,是这样儿的: 若p不是10的约数(即2和5) 时,当第i位到第n位组成的数%p==第j位到第n位组成的数%p,那么第i位到第j位上的数组成的数% ...

  5. Python 标准输出 sys.stdout 重定向(转)

    add by zhj: 其实很少使用sys.stdout,之前django的manage.py命令的源码中使用了sys.stdout和sys.stderr,所以专门查了一下 这两个命令与print的区 ...

  6. g++编译多个文件

    注意:头文件不用去指定,其是由#include命令进行管理的,只需要编译cpp文件就可以了: 举例: 有以下三个文件: a.h a.cpp main.cpp 那么编译可以有以下两种方式: 1.分开编译 ...

  7. Could not autowire. No beans of 'TbItemMapper' type found. less... (Ctrl+F1) Checks autowiring prob

    Intellij Idea开发工具在@Autowired或者@Resource注入XxxMapper接口时报如下错误: Could not autowire. No beans of 'TbItemM ...

  8. 【Loadrunner】Loadrunner 手动关联技术

    Loadrunner 手动关联技术 录制成功,回放失败,怀疑和动态数据有关: 1 重新录制一份脚本,两次录制的脚本进行比对,确定动态数据,复制动态数据: 2  找到第一次产生该动态数据的响应对应的相应 ...

  9. mysqldump备份数据出错

    收到nagios报警,提示mysql备份失败,线上使用的是逻辑备份,也就是使用mysqldump,由于数据比较小,也就没在乎速度神马的问题.好吧,那就查查是什么原因导致备份失败,由于备份是写成脚本定时 ...

  10. 解决无法连接到 reCAPTCHA 服务

    今天ytkah在查询一个信息时需要人机验证,但提示“无法连接到 reCAPTCHA 服务”,通过修改host文件可以解决相关问题,用editplus或notepad打开C:\Windows\Syste ...