Lambda是一个匿名函数,可以把Lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提升。
从匿名类到Lambda 的转换
Lambda
表达式在Java 语言中引入了一个新的语法元素和操作符。这个操作符为““->””,该操作符被称为Lambda 操作符或剪头操作符。它将Lambda 分为两个部分:
左侧:指定了Lambda 表达式需要的所有参数
右侧:指定了Lambda 体,即Lambda 表达式要执行的功能。
语法格式一:无参,无返回值,Lambda 体只需一条语句
Runnable r1=()->System.out.println("Hello Lambda!");
@Test
public void test1(){
//通过匿名内部类的方式实现接口
Runnable r=new Runnable() {
@Override
public void run() {
System.out.println("Hello World!");
}
};
r.run();
System.out.println("----------------------");
//匿名内部类用代替匿名内部类
Runnable r1=()->System.out.println("Hello Lambda!");
r1.run();
}
语法格式二:Lambda 需要一个参数
Consumer<String> con=(args)->System.out.println(args);
@Test
public void test2(){
Consumer<String> con=(args)->System.out.println(args);//对Consumer接口中有一个参数的accept方法的实现
con.accept("啦啦啦");
}
语法格式三:Lambda 只需要一个参数时,参数的小括号可以省略
Consumer<String> con=args->System.out.println(args);
语法格式四:Lambda 需要两个参数,并且有返回值
BinaryOperator<Long> bo = (Long x, Long y) -> {
System.out.println("实现函数接口方法!");
return x + y;
};
@Test
public void test3(){
Comparator<Integer> com=(x,y)->{
System.out.println("函数式接口");
return Integer.compare(x, y);
};
}
语法格式五:当Lambda 体只有一条语句时,return 与大括号可以省略
BinaryOperator<Long> bo = (x, y) -> x + y;
@Test
public void test4(){
Comparator<Integer> com=(x,y)->Integer.compare(x, y);
}
语法格式六:Lambda表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
BinaryOperator<Long> bo = (x, y) -> {
System.out.println("实现函数接口方法!");
return x + y;
};
(Integer x,Integer y)->Integer.compare(x,y);
上述Lambda 表达式中的参数类型都是由编译器推断得出的。Lambda 表达式中无需指定类型,程序依然可以编译,这是因为javac 根据程序的上下文,在后台推断出了参数的类型。Lambda 表达式的类型依赖于上下文环境,是由编译器推断出来的。这就是所谓的类型推断
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> ts = new TreeSet<>(com);
}
//Lambda表达式
@Test
public void test2() {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
TreeSet<Integer> ts = new TreeSet<>(com);
}
List<Employee> employees = Arrays.asList(
new Employee("张三", 18, 9496.2),
new Employee("李四", 52, 2396.2),
new Employee("王五", 56, 996.2),
new Employee("赵六", 8, 94.2)
);
@Test
public void test3() {
//需求:获取当前公司中员工年龄大于35的员工信息
List<Employee> emps = filterEmplyees1(employees);
for (Employee e : emps) {
System.out.println(e);
}
System.out.println("---------------------");
//需求:获取当前公司中员工工资大于2000的员工信息
List<Employee> emps2 = filterEmplyees2(employees);
for (Employee e : emps2) {
System.out.println(e);
}
}
public List<Employee> filterEmplyees1(List<Employee> list) {
List<Employee> emps = new ArrayList<Employee>();
for (Employee emp : list) {
if (emp.getAge() >= 35) {
emps.add(emp);
}
}
return emps;
}
public List<Employee> filterEmplyees2(List<Employee> list) {
List<Employee> emps = new ArrayList<Employee>();
for (Employee emp : list) {
if (emp.getSalary() >= 2000) {
emps.add(emp);
}
}
return emps;
}
//优化方式一:策略设计模式
@Test
public void test4() {
List<Employee> emps = filterEmplyees(employees, new FilterEmployeeByAge());
for (Employee e : emps) {
System.out.println(e);
}
System.out.println("---------------------");
List<Employee> emps2 = filterEmplyees(employees, new FilterEmployeeBySalary());
for (Employee e : emps2) {
System.out.println(e);
}
}
public List<Employee> filterEmplyees(List<Employee> list, MyPredicate<Employee> myPredicate) {
List<Employee> emps = new ArrayList<Employee>();
for (Employee emp : list) {
if (myPredicate.test(emp)) {
emps.add(emp);
}
}
return emps;
}
//优化方式二:匿名内部类
@Test
public void test5() {
List<Employee> list = filterEmplyees(employees, new MyPredicate<Employee>() {
@Override
public boolean test(Employee t) {
return t.getSalary() >= 2000;
}
});
for (Employee employee : list) {
System.out.println(employee);
}
}
//优化方式三:Lambda表达式
@Test
public void test6() {
List<Employee> list = filterEmplyees(employees, (e) -> e.getSalary() >= 2000);
list.forEach(System.out::println);
}
//优化方式四:stream API
@Test
public void test7() {
employees.stream()
.filter((e) -> e.getSalary() >= 2000)
.forEach(System.out::println);
System.out.println("------------------");
employees.stream()
.map(Employee::getName)
.forEach(System.out::println);
}
}
public interface MyPredicate<T> {
public boolean test(T t);
}
public class FilterEmployeeBySalary implements MyPredicate<Employee>{
@Override
public boolean test(Employee t) {
return t.getSalary()>=2000;
}
}