Lambda表达式基础语法
/*
* 一、Lambda 表达式的基础语法:Java8中引入了一个新的操作符 "->"
* 该操作符称为箭头操作符或 Lambda 操作符,箭头操作符将 Lambda 表达式拆分成两部分:
*
* 左侧:Lambda 表达式的参数列表
* 右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体
*
* 语法格式一:无参数,无返回值
* () -> System.out.println("Hello Lambda!");
*
* 语法格式二:有一个参数,并且无返回值
* (x) -> System.out.println(x)
*
* 语法格式三:若只有一个参数,小括号可以省略不写
* x -> System.out.println(x)
*
* 语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
* Comparator<Integer> com = (x, y) -> {
* System.out.println("函数式接口");
* return Integer.compare(x, y);
* };
*
* 语法格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
* Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
*
* 语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
* (Integer x, Integer y) -> Integer.compare(x, y);
*
*
* 二、Lambda 表达式需要“函数式接口”的支持
* 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。
* 可以使用注解@FunctionalInterface 修饰,此注解可以检查是否是函数式接口
*/
语法格式一:无参数,无返回值
public class TestLDemo {
@Test
public void test1() {
int num = 0;//jdk 1.8 前,必须是显示地写出 final 1.8后可以省略finaldan但是仍然是不可变的 不能进行num++操作
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("匿名内部类:无参数,无返回值:" + num);
}
};
r.run();
System.out.println("------------------------------------------------------------");
Runnable rlambda = () -> System.out.println("语法格式一:无参数,无返回值:"+num);
rlambda.run();
}
}
语法格式二:有一个参数,并且无返回值
public class TestLDemo {
@Test
public void test1() {
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String str) {
System.out.println(str);
}
};
consumer.accept("匿名内部类测试:语法格式二:有一个参数,并且无返回值");
System.out.println("----------------------------------------------");
Consumer<String> consumerLambda = (str)-> System.out.println(str);
consumerLambda.accept("语法格式二:有一个参数,并且无返回值");
}
}
语法格式三:若只有一个参数,小括号可以省略不写
public class TestLDemo {
@Test
public void test1() {
Consumer<String> cu = str-> System.out.println(str);
cu.accept("语法格式三:若只有一个参数,小括号可以省略不写");
}
}
语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
public class TestLDemo {
@Test
public void test1() {
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
System.out.println("匿名内部类测试:语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句");
return Integer.compare(o1,o2);
}
};
int compare = comparator.compare(1, 2);
System.out.println(compare);
System.out.println("----------------------------------------------");
Comparator<Integer> comparatorLambda = (x,y)->{
System.out.println("语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句");
return Integer.compare(x, y);
};
int compareLambda = comparatorLambda.compare(2, 2);
System.out.println(compareLambda);
}
}
语法格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
public class TestLDemo {
@Test
public void test1() {
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
}
}
语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
@FunctionalInterface
public interface IMyNumCalculation<T, R> {
public R calculation(T t1, T t2);
// public R calculationTest(T t1, T t2); @FunctionalInterface 修饰只能有一个抽象方法
}
public class TestLDemo {
@Test
public void test1() {
IMyNumCalculation<Integer, Integer> calculation = (x, y) -> {
System.out.println("两数相乘");
return x * y;
};
Integer result = calculation.calculation(6, 8);
System.out.println(result);
}
}
public class TestLDemo {
@Test
public void test1() {
Integer result = calculation(6,8,((x, y) -> x * y));
System.out.println(result);
Integer result1 = calculation(6, 8, ((x, y) -> x + y));
System.out.println(result1);
}
public Integer calculation(Integer t1, Integer t2, IMyNumCalculation<Integer, Integer> calculation){
return calculation.calculation(t1, t2);
}
}
Lambda表达式有什么用处
https://blog.csdn.net/FL63Zv9Zou86950w/article/details/91467997