1. Lambda表达式使用前后的对比:
举例一:
- 代码示例:
@Test
public void test1(){
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("我爱北京天安门");
}
};
r1.run();//我爱北京天安门
System.out.println("******************************");
Runnable r2 = () -> System.out.println("我爱北京故宫");
r2.run();//我爱北京故宫
}
举例二:
- 代码示例:
@Test
public void test2(){
Comparator<Integer> com1 = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
};
int compare1 = com1.compare(12, 21);
System.out.println(compare1);//-1
System.out.println("******************************");
//Lambda表达式的写法
Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1,o2);
int compare2 = com2.compare(32, 21);
System.out.println(compare2);//1
System.out.println("******************************");
//方法引用
Comparator<Integer> com3 = Integer :: compare;
int compare3 = com3.compare(32, 21);
System.out.println(compare3);//1
}
2. Lambda表达式的基本语法:
-
- 举例:
(o1,o2) -> Integer.compare(o1,o2);
- 举例:
-
- 格式:
-> :lambda操作符 或 箭头操作符
-> :左边:lambda形参列表(其实就是接口中的抽象方法的形参列表)
-> :右边:lambda体(其实就是重写抽象方法的方法体)
3. 如何使用:分为六种情况
- 代码示例:
//语法格式一:无参,无返回值
@Test
public void test1(){
Runnable r1 = new Runnable() {
@Override
public void run() {
System.out.println("我爱北京天安门");
}
};
r1.run();//我爱北京天安门
System.out.println("******************************");
Runnable r2 = () -> System.out.println("我爱北京故宫");
r2.run();//我爱北京故宫
}
//语法格式二:lambda 需要一个参数,但是没有返回值。
@Test
public void test2(){
Consumer<String> con = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
con.accept("谎言和誓言的区别是什么?");
System.out.println("*********************************");
Consumer<String> con1 = (String s) -> {
System.out.println(s);
};
con1.accept("一个是听的人当真了,一个是说的人当真了");
}
//语法格式三:数据类型可以省略,因为可由编译器推断得出,称为“类型推断”
@Test
public void test3(){
Consumer<String> con1 = (String s) -> {
System.out.println(s);
};
con1.accept("一个是听的人当真了,一个是说的人当真了");
System.out.println("********************************");
Consumer<String> con2 = (s) -> {
System.out.println(s);
};
con2.accept("一个是听的人当真了,一个是说的人当真了");
}
@Test
public void test4(){
ArrayList<String> list = new ArrayList<>();//类型推断
int[] arr = {1,2,3};//类型推断
}
//语法格式四:lambda 若只需要一个参数时,参数的小括号可以省略
@Test
public void test5(){
Consumer<String> con1 = (s) -> {
System.out.println(s);
};
con1.accept("一个是听的人当真了,一个是说的人当真了");
System.out.println("********************************");
Consumer<String> con2 = s -> {
System.out.println(s);
};
con2.accept("一个是听的人当真了,一个是说的人当真了");
}
//语法格式五:lambda 需要两个或以上的参数,多条执行语句,并且可以有返回值
@Test
public void test6(){
Comparator<Integer> com1 = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
}
};
System.out.println(com1.compare(12, 21));
System.out.println("********************************");
Comparator<Integer> com2 = (o1,o2) -> {
System.out.println(o1);
System.out.println(o2);
return o1.compareTo(o2);
};
System.out.println(com2.compare(12, 6));
}
//语法格式六:当lambda 体只有一条语句时,return与大括号若有,都可以省略
@Test
public void test7(){
Comparator<Integer> com1 = (o1,o2) -> {
return o1.compareTo(o2);
};
System.out.println(com1.compare(12, 6));
System.out.println("*******************************");
Comparator<Integer> com2 = (o1,o2) -> o1.compareTo(o2);
System.out.println(com2.compare(12, 21));
}
总结六种情况:
-
-> 左边 :lambda形参列表的参数类型可以省略(类型推断);如果lambda形参列表只有一个参数,其一对()也可以省略
-
-> 右边 :lambda体应该使用一对{}包裹;如果lambda体只有一条执行语句(可能是return语句),可以省略这一对{}和return关键字