JDK8
Posted xuweiweiwoaini
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDK8相关的知识,希望对你有一定的参考价值。
1 Java8新特性的简介
- 速度更快
- 代码更少(增加了新语法Lambda表达式)
- 强大的Stream API
- 便于并行
- 最大化的减少空指针异常 Optional
- 其中最为核心的为Lambda表达式和Stream API
2 为什么要使用Lambda表达式
2.1 Lambda表达式的简介
- Lambda是一个匿名函数,我们可以把Lambda表达式理解为一段可以传递的代码。
- Lambda表达式可以写出更简洁、更灵活的代码。
- Lambda表达式作为一种更紧凑的代码风格,使得java语言的表达能力得到了提升。
2.2 Lambda表达式的演进
2.2.1 使用匿名内部类来实现自定义比较器
- 示例:
package com.xuweiwei.java8; import org.junit.Test; import java.util.Comparator; public class TestLambda { @Test public void test1(){ Comparator<Integer> comparator = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return Integer.compare(01,02); } }; } }
2.2.2 使用Lambda表达式来实现自定义比较器
- 示例:
package com.xuweiwei.java8; import org.junit.Test; import java.util.Comparator; public class TestLambda { @Test public void test2(){ Comparator<Integer> comparator = (x,y) -> Integer.compare(x,y); } }
- 示例:
package com.xuweiwei.java8; import org.junit.Test; import java.util.Comparator; public class TestLambda { @Test public void test2(){ Comparator<Integer> comparator = Integer::compare; } }
2.2.3 获取当前公司中员工年龄>35岁的员工信息
- 示例:
package com.xuweiwei.java8; public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer 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 + ‘}‘; } }
package com.xuweiwei.java8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三", 18, 9999.99), new Employee("李四", 38, 1111.11), new Employee("王五", 90, 5555.55), new Employee("赵六", 8, 3333.5), new Employee("田七", 16, 111.85), new Employee("王八", 45, 999.45) ); @Test public void test3() { List<Employee> employees = filterEmployees(this.employees); for (Employee employee : employees) { System.out.println(employee); } } /** * 获取当前公司中员工年龄>35岁的员工信息 * * @param employees * @return */ public List<Employee> filterEmployees(List<Employee> employees) { List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if (employee.getAge() > 35) { result.add(employee); } } return result; } }
2.2.4 获取当前公司中员工薪水>5000的员工信息
- 示例:
package com.xuweiwei.java8; public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer 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 + ‘}‘; } }
package com.xuweiwei.java8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三", 18, 9999.99), new Employee("李四", 38, 1111.11), new Employee("王五", 90, 5555.55), new Employee("赵六", 8, 3333.5), new Employee("田七", 16, 111.85), new Employee("王八", 45, 999.45) ); @Test public void test3() { List<Employee> employees = filterEmployees(this.employees); for (Employee employee : employees) { System.out.println(employee); } } /** * 获取当前公司中员工薪水>5000的员工信息 * * @param employees * @return */ public List<Employee> filterEmployees(List<Employee> employees) { List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if (employee.getSalary() > 5000) { result.add(employee); } } return result; } }
2.2.5 使用策略模式来改写2.2.3
- 示例:
package com.xuweiwei.java8; public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer 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 + ‘}‘; } }
package com.xuweiwei.java8; public interface MyPredicate<T> { boolean test(T t); }
package com.xuweiwei.java8; public class FilterEmployeeByAge implements MyPredicate<Employee> { @Override public boolean test(Employee employee) { return employee.getAge() > 35; } }
package com.xuweiwei.java8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三", 18, 9999.99), new Employee("李四", 38, 1111.11), new Employee("王五", 90, 5555.55), new Employee("赵六", 8, 3333.5), new Employee("田七", 16, 111.85), new Employee("王八", 45, 999.45) ); @Test public void test3() { List<Employee> employees = filterEmployees(this.employees,new FilterEmployeeByAge()); for (Employee employee : employees) { System.out.println(employee); } } /** * 获取当前公司中员工年龄>35岁的员工信息 * * @param employees * @return */ public List<Employee> filterEmployees(List<Employee> employees,MyPredicate<Employee> myPredicate) { List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if (myPredicate.test(employee)) { result.add(employee); } } return result; } }
2.2.6 使用策略模式来改写2.2.4
- 示例:
package com.xuweiwei.java8; public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer 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 + ‘}‘; } }
package com.xuweiwei.java8; public interface MyPredicate<T> { boolean test(T t); }
package com.xuweiwei.java8; public class FilterEmployeeBySalary implements MyPredicate<Employee> { @Override public boolean test(Employee employee) { return employee.getSalary() > 5000; } }
package com.xuweiwei.java8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三", 18, 9999.99), new Employee("李四", 38, 1111.11), new Employee("王五", 90, 5555.55), new Employee("赵六", 8, 3333.5), new Employee("田七", 16, 111.85), new Employee("王八", 45, 999.45) ); @Test public void test3() { List<Employee> employees = filterEmployees(this.employees,new FilterEmployeeBySalary()); for (Employee employee : employees) { System.out.println(employee); } } /** * 获取当前公司中员工薪水>5000的员工信息 * * @param employees * @return */ public List<Employee> filterEmployees(List<Employee> employees,MyPredicate<Employee> myPredicate) { List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if (myPredicate.test(employee)) { result.add(employee); } } return result; } }
2.2.7 使用匿名内部类改写2.2.5
- 示例:
package com.xuweiwei.java8; public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer 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 + ‘}‘; } }
package com.xuweiwei.java8; public interface MyPredicate<T> { boolean test(T t); }
package com.xuweiwei.java8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三", 18, 9999.99), new Employee("李四", 38, 1111.11), new Employee("王五", 90, 5555.55), new Employee("赵六", 8, 3333.5), new Employee("田七", 16, 111.85), new Employee("王八", 45, 999.45) ); @Test public void test3() { List<Employee> employees = filterEmployees(this.employees, new MyPredicate<Employee>() { @Override public boolean test(Employee employee) { return employee.getAge() > 35; } }); for (Employee employee : employees) { System.out.println(employee); } } /** * 获取当前公司中员工年龄>35岁的员工信息 * * @param employees * @return */ public List<Employee> filterEmployees(List<Employee> employees, MyPredicate<Employee> myPredicate) { List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if (myPredicate.test(employee)) { result.add(employee); } } return result; } }
2.2.8 使用Lambda表达式改写2.2.5
- 示例:
package com.xuweiwei.java8; public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer 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 + ‘}‘; } }
package com.xuweiwei.java8; public interface MyPredicate<T> { boolean test(T t); }
package com.xuweiwei.java8; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三", 18, 9999.99), new Employee("李四", 38, 1111.11), new Employee("王五", 90, 5555.55), new Employee("赵六", 8, 3333.5), new Employee("田七", 16, 111.85), new Employee("王八", 45, 999.45) ); @Test public void test3() { List<Employee> employees = filterEmployees(this.employees, (e) -> e.getAge() > 35); for (Employee employee : employees) { System.out.println(employee); } } /** * 获取当前公司中员工年龄>35岁的员工信息 * * @param employees * @return */ public List<Employee> filterEmployees(List<Employee> employees, MyPredicate<Employee> myPredicate) { List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if (myPredicate.test(employee)) { result.add(employee); } } return result; } }
以上是关于JDK8的主要内容,如果未能解决你的问题,请参考以下文章