Java8-01-笔记
Posted 寻7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8-01-笔记相关的知识,希望对你有一定的参考价值。
Java8-01-笔记
Lambda表达式
1、引入案例
-
Lambda是一个匿名函数,我们可以把Lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到提升。
-
初步感受一下lambda表达式,下面定义的两个TreeSet对象ts1和ts2使用的比较器是一样的。
package com.sunstone.lambda; import org.junit.Test; import java.util.Comparator; import java.util.TreeSet; public class TestLambda //原来的匿名内部类 @Test public void test1() Comparator<Integer> com = new Comparator<Integer>() @Override public int compare(Integer o1, Integer o2) return Integer.compare(o1,o2); ; TreeSet<Integer> ts1 = new TreeSet<>(com); //Lambda表达式 public void test2() Comparator<Integer> com = (x,y) -> Integer.compare(x,y); TreeSet<Integer> ts2 = new TreeSet<>(com);
-
需求:按条件查询对应员工信息
package com.sunstone.lambda; //员工实体类 public class Employee private String name; private int age; private double salary; public Employee() public Employee(String name, int 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 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 + '';
-
通过下面的两个需求我们能够看出按工资条件查找和按年龄条件查找员工方法相似,代码重复,使用四种方式进行优化。
package com.sunstone.lambda; import org.junit.Test; import java.util.*; public class TestLambda List<Employee> emps = Arrays.asList( new Employee("小孙",17,5555.55), new Employee("小思",27,3333.33), new Employee("小东",37,9999.99), new Employee("小零",50,7777.77), new Employee("小雪",7,8888.88), new Employee("小豹",60,6666.66) ); //需求1:获取公司中工资大于5000的员工 public List<Employee> filterEmployeeSalary(List<Employee> emps) List<Employee> list = new ArrayList<Employee>(); for (Employee emp:emps) if(emp.getSalary()>=5000) list.add(emp); return list; @Test public void test3() List<Employee> list =filterEmployeeSalary(emps); for (Employee employee:list) System.out.println(employee); //需求2:获取公司中年龄小于35的员工 public List<Employee> filterEmployeeAge(List<Employee> emps) List<Employee> list = new ArrayList<Employee>(); for (Employee emp:emps) if(emp.getAge()<=35) list.add(emp); return list; @Test public void test4() List<Employee> list =filterEmployeeAge(emps); for (Employee employee:list) System.out.println(employee);
-
优化方式一:策略设计模式
package com.sunstone.lambda; @FunctionalInterface //函数式接口 意思就是表明这个接口就只有一个抽象方法 public interface MyPredicate<T> public boolean test(T t); //@111@
package com.sunstone.lambda; public class FilterEmployeeForSalary implements MyPredicate<Employee> @Override public boolean test(Employee employee) //@333@ return employee.getSalary()>=5000;
package com.sunstone.lambda; public class FilterEmployeeForAge implements MyPredicate<Employee> @Override public boolean test(Employee employee) //@333@ return employee.getAge()<=35;
package com.sunstone.lambda; import org.junit.Test; import java.util.*; public class TestLambda List<Employee> emps = Arrays.asList( new Employee("小孙",17,5555.55), new Employee("小思",27,3333.33), new Employee("小东",37,9999.99), new Employee("小零",50,7777.77), new Employee("小雪",7,8888.88), new Employee("小豹",60,6666.66) ); public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp) List<Employee> list = new ArrayList<>(); for (Employee employee : emps) if(mp.test(employee)) //@222@ list.add(employee); return list; @Test public void test5() List<Employee> list1 =filterEmployee(emps, new FilterEmployeeForAge()); //@333@ for (Employee employee:list1) System.out.println(employee); List<Employee> list2 =filterEmployee(emps, new FilterEmployeeForSalary()); //@333@ for (Employee employee:list2) System.out.println(employee);
-
优化方式二:匿名内部类
package com.sunstone.lambda; @FunctionalInterface public interface MyPredicate<T> public boolean test(T t); //@111@
package com.sunstone.lambda; import org.junit.Test; import java.util.*; public class TestLambda List<Employee> emps = Arrays.asList( new Employee("小孙",17,5555.55), new Employee("小思",27,3333.33), new Employee("小东",37,9999.99), new Employee("小零",50,7777.77), new Employee("小雪",7,8888.88), new Employee("小豹",60,6666.66) ); public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp) List<Employee> list = new ArrayList<>(); for (Employee employee : emps) //@222@ if(mp.test(employee)) list.add(employee); return list; @Test public void test6() List<Employee> list1 = filterEmployee(emps, new MyPredicate<Employee>() @Override public boolean test(Employee employee) //@333@ return employee.getSalary() >= 5000; ); for (Employee employee:list1) System.out.println(employee); System.out.println("======================="); List<Employee> list2 = filterEmployee(emps, new MyPredicate<Employee>() @Override public boolean test(Employee employee) //@333@ return employee.getAge() <= 35; ); list2.forEach(employee -> System.out.println(employee));
-
优化方式三:Lambda表达式
package com.sunstone.lambda; @FunctionalInterface public interface MyPredicate<T> public boolean test(T t); //@111@
package com.sunstone.lambda; import org.junit.Test; import java.util.*; public class TestLambda List<Employee> emps = Arrays.asList( new Employee("小孙",17,5555.55), new Employee("小思",27,3333.33), new Employee("小东",37,9999.99), new Employee("小零",50,7777.77), new Employee("小雪",7,8888.88), new Employee("小豹",60,6666.66) ); public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp) List<Employee> list = new ArrayList<>(); for (Employee employee : emps) //@222@ if(mp.test(employee)) list.add(employee); return list; @Test public void test7() List<Employee> list1 = filterEmployee(emps, emp -> emp.getSalary() >= 5000);//@333@ list1.forEach(employee -> System.out.println(employee)); System.out.println("======================="); List<Employee> list2 = filterEmployee(emps, emp -> emp.getAge() <= 35); //@333@ list2.forEach(System.out :: println);
-
优化方式四:Stream API
package com.sunstone.lambda; import org.junit.Test; import java.util.*; public class TestLambda List<Employee> emps = Arrays.asList( new Employee("小孙",17,5555.55), new Employee("小思",27,3333.33), new Employee("小东",37,9999.99), new Employee("小零",50,7777.77), new Employee("小雪",7,8888.88), new Employee("小豹",60,6666.66) ); //@111@ Java8内置了四大核心函数式接口 //@222@ stream流中的方法入参是函数式接口的实现类 @Test public void test8() emps.stream() .filter(employee -> employee.getSalary() >= 5000) //@333@ .forEach(System.out::println); System.out.println("======================="); emps.stream() .filter(employee -> employee.getAge() <= 35) //@333@ .forEach(System.out::println);
2、基本语法<
以上是关于Java8-01-笔记的主要内容,如果未能解决你的问题,请参考以下文章