Java8相较于之前的版本有哪些优点:
1、运行速度更快(内存管理和垃圾回收机制发生了改变)
2、代码更简洁(因为使用了Lamda表达式)
3、强大的Stream API
4、便于并行
5、使用Optional可减少空指针异常
最为核心的的两大特性:Lamda表达式和Stream API
?如何理解Lamda表达式?
Lambda将功能实现(即模型中的 {代码块})作为参数进行传递。
Lambda式子中,"->“前面的是需要实现的抽象方法的形参,没有形参就”()"为空处理,后面的是实现抽象方法的方法体。即(可省略类型,也可省略参数的参数列表)->{ 代码块 }
如下是Lamda表达式的代码转换模型图:
?最简单的Lamda演示实例:
// 原来的匿名内部类
public void test01() {
Comparator<Integer> com = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
};
}
// Lamda 表达式
public void test02() {
Comparator<Integer> com1 = (x,y) -> {return Integer.compare(x, y);};
Comparator<Integer> com2 = (x,y) -> Integer.compare(x, y);
}
使用Java8的语法特性优化代码
List<Employee> employeeList = Arrays.asList(
new Employee("张三",18,3000),
new Employee("李四",28,8000),
new Employee("王五",38,9000),
new Employee("陈六",48,7000),
new Employee("李七",58,2000)
);
private List<Employee> filterEmployee(List<Employee> list, MyPredicate predicate){
List<Employee> result = new ArrayList<>();
for(Employee emp : list){
if(predicate.filter(emp)){
result.add(emp);
}
}
return result;
}
// 传统的写法
@Test
public void test01(){
// 过滤出年龄于50的
List<Employee> list01 = new ArrayList<>();
for(Employee employee : employeeList){
if(employee.getAge() > 50){
list01.add(employee);
}
}
for(Employee employee : list01){
System.out.println(employee);
}
// 过滤出工资大于4000的
List<Employee> list02 = new ArrayList<>();
for(Employee employee : employeeList){
if(employee.getSalary() > 5000){
list02.add(employee);
}
}
for(Employee employee : list02){
System.out.println(employee);
}
}
// 优化方式一:使用设计模式之:策略模式
@Test
public void test02(){
List<Employee> list01 = filterEmployee(employeeList, new EmployeeFilterByAge());
for (Employee emp : list01) {
System.out.println(emp);
}
System.out.println("--------------------------------");
List<Employee> list02 = filterEmployee(employeeList, new EmployeeFilterBySalary());
for (Employee emp : list02) {
System.out.println(emp);
}
}
// 优化方式二:使用匿名内部类
@Test
public void test03(){
List<Employee> list = filterEmployee(employeeList, new MyPredicate<Employee>() {
@Override
public boolean filter(Employee emp) {
return emp.getAge() > 50;
}
});
for (Employee emp : list) {
System.out.println(emp);
}
}
// 优化方式三:使用Lambda表达式
@Test
public void test04(){
List<Employee> list = filterEmployee(employeeList, (MyPredicate<Employee>) emp -> emp.getAge() > 50);
list.forEach(System.out::println);
}
// 优化方式四:使用Stream API
@Test
public void test05(){
employeeList.stream().filter((e) -> e.getAge() >50).forEach(System.out::println);
}
相关类的定义
public class Employee {
public Employee(String name, int age, int salary) {
this.name = name;
this.age = age;
this.salary = salary;
}
public String name;
public int age;
public int salary;
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 int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
'}';
}
}
public interface MyPredicate<T> {
public boolean filter(T t);
}
public class EmployeeFilterByAge implements MyPredicate<Employee> {
@Override
public boolean filter(Employee employee) {
return false;
}
}
public class EmployeeFilterBySalary implements MyPredicate<Employee> {
@Override
public boolean filter(Employee emp) {
return emp.salary > 5000;
}
}
|