速度更快;
代码更少;
强大的streamAPI;
便于并行;
最大减少空指针异常Optional;
其中最核心的lambda表达式和StreamAPI。
lambda是一个匿名函数,可以把lambda表达式理解为一段可以传递的代码。可以写出更简洁更灵活的代码,使java语言的表达能力得到了提升。
1.Employee实体类
public class Employee { private String userName; private int age; private int salary; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } 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{" + "userName=‘" + userName + ‘\‘‘ + ", age=" + age + ", salary=" + salary + ‘}‘; } }
2.函数式接口
public interface MyPredicate { public boolean test(Employee employee); }
3.lambda实现
public class EmployeeMain { public List<Employee> filterEmployee(List<Employee> employees,MyPredicate mp){ List<Employee> result = new ArrayList<>(); for (Employee employee : employees) { if(mp.test(employee)){ result.add(employee); } } return result; } public static void main(String[] args) { List<Employee> employees = new ArrayList<>(); for(int i = 0;i<5;i++){ Employee employee = new Employee(); employee.setUserName("zhangsan"+i); employee.setAge(48+i); employee.setSalary(2445+10*i); employees.add(employee); } EmployeeMain employeeMain = new EmployeeMain(); System.out.println("--------------------------------"); //lambda实现 List<Employee> employeesByLambda = employeeMain.filterEmployee(employees,(e)->e.getSalary()>2450); for (Employee employee : employeesByLambda) { System.out.println(employee.toString()); } System.out.println("--------------------------------"); //lambda按age筛选实现 List<Employee> employeesByLambdaWithAge = employeeMain.filterEmployee(employees,(e)-> e.getAge()>50); for (Employee employee : employeesByLambdaWithAge) { System.out.println(employee.toString()); } } }
4.执行结果:
java8中引入了一个新的操作符“->”,该操作符称为箭头操作符lambda操作符,箭头操作符将lambda表达式拆分为两部分。其中,lambda操作符左侧为参数列表,右侧为lambda表达式所执行的功能,即lambda体。lambda需要声明函数式接口,函数式接口即只有一个抽象方法的接口。
语法格式大致分为:
public static void main(String[] args) { /** * 无参数,无返回值 */ Runnable runnable = ()-> System.out.println("线程lambda表达式"); runnable.run(); }
public static void main(String[] args) { /** * 一个参数,无返回值 */ Consumer<Integer> consumer = x-> System.out.println(x); consumer.accept(10); }
public static void main(String[] args) { /** * 两个参数,且有返回值,只有一条实现语句 */ System.out.println("------------------------"); Comparator<Integer> comparator = (x,y)->Integer.compare(x,y); System.out.println(comparator.compare(10,20)); }
public static void main(String[] args) { Comparator<Integer> comparator1 = (x,y)->{ System.out.println("多条语句"); return Integer.compare(x,y); }; System.out.println(comparator1.compare(11,2)); }
1.声明函数式接口
@FunctionalInterface //声明为函数式接口 public interface MyFun { public Integer getValue(int num); }
2.定义一个operation函数
public class TestLambda { public Integer operation(int num,MyFun myFun){ return myFun.getValue(num); } @Test public void test(){ Integer num = operation(100,x->x*x); System.out.println(num); } }
原文:https://www.cnblogs.com/menbo/p/13636254.html