JDK8新特性01 Lambda表达式01_设计的由来
1.java bean
public class Employee {
private int id;
private String name;
private int age;
private double salary;
public Employee() {
}
public Employee(String name) {
this.name = name;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public String show() {
return "测试方法引用!";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(salary);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
return true;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
2.早期Java版本的设计策略和8版本的Lambda的接口类:
public interface MyPredicate<T> {
public boolean test(T t);
}
//2.策略模式的接口实现类1
public class FilterEmployeeForAge implements MyPredicate<Employee>{
@Override
public boolean test(Employee t) {
return t.getAge() <= 35;
}
}
//3.策略模式的接口实现类2
public class FilterEmployeeForSalary implements MyPredicate<Employee> {
@Override
public boolean test(Employee t) {
return t.getSalary() >= 5000;
}
}
//4.lambda表达式定义的接口
@FunctionalInterface
public interface MyFun {
public Integer getValue(Integer num);
}
3.原来的实现以及现在的Lambda的优化
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet; import org.junit.Test; public class TestLambda1 { //原来的匿名内部类
@Test
public void test1(){
Comparator<String> com = new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
return Integer.compare(o1.length(), o2.length());
}
}; TreeSet<String> ts = new TreeSet<>(com); TreeSet<String> ts2 = new TreeSet<>(new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
return Integer.compare(o1.length(), o2.length());
} });
} //现在的 Lambda 表达式
@Test
public void test2(){
Comparator<String> com = (x, y) -> Integer.compare(x.length(), y.length());
TreeSet<String> ts = new TreeSet<>(com);
} List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55)
); //需求:获取公司中年龄小于 35 的员工信息
public List<Employee> filterEmployeeAge(List<Employee> emps){
List<Employee> list = new ArrayList<>(); for (Employee emp : emps) {
if(emp.getAge() <= 35){
list.add(emp);
}
} return list;
} @Test
public void test3(){
List<Employee> list = filterEmployeeAge(emps); for (Employee employee : list) {
System.out.println(employee);
}
} //需求:获取公司中工资大于 5000 的员工信息
public List<Employee> filterEmployeeSalary(List<Employee> emps){
List<Employee> list = new ArrayList<>(); for (Employee emp : emps) {
if(emp.getSalary() >= 5000){
list.add(emp);
}
} return list;
} //优化方式一:策略设计模式
public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp){
List<Employee> list = new ArrayList<>(); for (Employee employee : emps) {
if(mp.test(employee)){
list.add(employee);
}
} return list;
} //策略模式的调用
@Test
public void test4(){
//策略1
List<Employee> list = filterEmployee(emps, new FilterEmployeeForAge());
for (Employee employee : list) {
System.out.println(employee);
} System.out.println("------------------------------------------"); //策略2
List<Employee> list2 = filterEmployee(emps, new FilterEmployeeForSalary());
for (Employee employee : list2) {
System.out.println(employee);
}
} //优化方式二:匿名内部类.(策略模式每次都需要创建新的实现类,麻烦)
@Test
public void test5(){
List<Employee> list = filterEmployee(emps, new MyPredicate<Employee>() {
@Override
public boolean test(Employee t) {
return t.getId() <= 103;
}
}); for (Employee employee : list) {
System.out.println(employee);
}
} //优化方式三:Lambda 表达式
//精简匿名内部类的实现,将方法的实现在方法的参数中传递,不再new
@Test
public void test6(){
List<Employee> list = filterEmployee(emps, (e) -> e.getAge() <= 35);
list.forEach(System.out::println); System.out.println("------------------------------------------"); List<Employee> list2 = filterEmployee(emps, (e) -> e.getSalary() >= 5000);
list2.forEach(System.out::println);
} //优化方式四:Stream API
@Test
public void test7(){
emps.stream()
.filter((e) -> e.getAge() <= 35)
.forEach(System.out::println); System.out.println("----------------------------------------------"); emps.stream()
.map(Employee::getName)
.limit(3)
.sorted()
.forEach(System.out::println);
}
}
JDK8新特性01 Lambda表达式01_设计的由来的更多相关文章
- JDK8新特性(一) Lambda表达式及相关特性
函数式接口 函数式接口是1.8中的新特性,他不属于新语法,更像是一种规范 面向对象接口复习 在这里先回顾一下面向对象的接口,创建接口的关键字为interface,这里创建一个日志接口: public ...
- JDK8新特性之Lambda表达式
Lambda表达式主要是替换了原有匿名内部类的写法,也就是简化了匿名内部类的写法.lambda语法结构: (参数1,参数2...)->{重写方法的内容,不定义方法名} 先看一个使用匿名内部类定义 ...
- JDK8新特性02 Lambda表达式02_Lambda语法规则
//函数式接口:只有一个抽象方法的接口称为函数式接口. 可以使用注解 @FunctionalInterface 修饰 @FunctionalInterface public interface MyF ...
- jdk8新特性--使用lambda表达式的延迟执行特性优化性能
使用lambda表达式的延迟加载特性对代码进行优化:
- JDK8新特性:Lambda表达式
Lambda表达式,案例一:new Thread(() -> System.out.println("thread")); Lambda表达式,案例二:由参数/箭头和主体组成 ...
- JDK8新特性03 Lambda表达式03_Java8 内置的四大核心函数式接口
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.functio ...
- java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合
java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合 比如,我有一张表: entity Category.java service CategoryServic ...
- Java8 新特性学习 Lambda表达式 和 Stream 用法案例
Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...
- C++11新特性之一——Lambda表达式
C++11新特性总结可以参考:http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html#section_6.8 C++ ...
随机推荐
- [国家集训队]middle 解题报告
[国家集训队]middle 主席树的想法感觉挺妙的,但是这题数据范围这么小,直接分块草过去不就好了吗 二分是要二分的,把\(<x\)置\(-1\),\(\ge x\)的置\(1\),于是我们需要 ...
- HR_Sherlock and Anagrams_TIMEOUT[UNDONE]
2019年1月10日15:39:23 去掉了所有不必要的循环区间 还是超时 本地运行大概3s #!/bin/python3 import math import os import random im ...
- 【php】php实现数组反转
php里面有个函数可以反转数组,工作中也经常用到,非常方便.今天来自己实现这样的功能. $arr = [2,5,6,1,8,16,12]; function reverse($arr){ $left ...
- prufer序列学习笔记
prufer序列是一个定义在无根树上的东西. 构造方法是:每次选一个编号最小的叶子结点,把他的父亲的编号加入到序列的最后.然后删掉这个叶节点.直到最后只剩下两个节点,此时得到的序列就是prufer序列 ...
- Docker部署Jenkins测试环境
安装docker环境 yum install epel-release -y && yum install docker -y 如果是高手需要docker-compose的话就再装个d ...
- centos7系统排错
系统排错 troubleshooting winPE --光盘或u盘启动盘 产生一个PE系统(类似内存上跑的临时系统) 系统排错 rescue 模式 (挽救模式) 类似windows winPE模式 ...
- 牛客寒假算法基础集训营3处女座和小姐姐(三) (数位dp)
链接:https://ac.nowcoder.com/acm/contest/329/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...
- 转:upload.parseRequest为空
上传是items一直是空list.导致原因是struts2把原始的原来S2为简化上传功能,把所有的enctype="multipart/form-data"表单做了wrapper最 ...
- 114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...
- 关键字(7):属性的增删改add,drop,modify
新建一张表: )); 注意:新建表时,表里面至少要有一个字段 删除整张表: drop table nac_user.a_bt; 增加表的一个属性: ) default('M') 新增外键 ...