前言:
身为一个小白,总是出现脑容量不足的情况,天天都在往期回顾的路上不停前进,就很苦恼。
一.Lambda表达式语法
函数式接口:
函数式接口是指只包含一个抽象方法的接口。在Java中,函数式接口是支持函数式编程的基础,它可以用作Lambda表达式的目标类型。
函数式接口具有以下特点:
-
只包含一个抽象方法:函数式接口只能有一个未实现的抽象方法,用于定义函数式接口的行为。
-
可以有默认方法和静态方法:函数式接口可以包含默认方法和静态方法,用于提供额外的功能。
-
可以使用注解@FunctionalInterface进行标记:使用@FunctionalInterface注解可以确保接口是函数式接口,如果接口不符合函数式接口的定义,编译器会报错。
函数式接口的存在使得Java可以支持函数式编程的特性,例如Lambda表达式、方法引用等。通过函数式接口,可以将函数作为一等公民来处理,可以将函数作为参数传递给其他方法,也可以将函数作为返回值返回。
Java中的一些常见的函数式接口包括:Runnable、Comparator、Consumer、Function、Predicate等。这些函数式接口提供了不同的功能,可以根据具体的需求选择合适的函数式接口来使用。
语法:
- (参数列表)-> {方法体}
- ()用来描述参数列表
- {} 用来描述方法体
- -> 运算符,可以叫做箭头符号或者goes to
代码示例:
接口方法中包含 无参数,单个参数,两个参数有返回值和无返回值的六种情况示例
package lambda;
/**
* Lambda语法示例
* DATE = 2023/7/26 22:12
*/
public class Test1 {
public static void main(String[] args) {
TestLambda1 l1 = ()->{
System.out.println("无参数无返回值");
};
l1.test();
TestLambda2 l2 = (int a) -> {
System.out.println("一个参数无返回值");
};
l2.test(5);
TestLambda3 l3 =(int a,int b)->{
System.out.println("两个参数无返回值"+(a+b));
};
l3.test(4, 7);
TestLambda4 l4 = ()->{
return 4;
};
System.out.println("无参数有返回值"+l4.test());
TestLambda5 l5 = (int a) -> {
return a;
};
System.out.println("一个参数有返回值"+l5.test(5));
TestLambda6 l6 = (int a,int b) -> {
return a+b;
};
System.out.println("两个参数有返回值"+l6.test(7,8));
}
/**
* 无参数无返回值
*/
interface TestLambda1{
void test();
}
/**
* 一个参数无返回值
*/
interface TestLambda2{
void test(int a);
}
/**
* 一个参数无返回值
*/
interface TestLambda3{
void test(int a,int b);
}
/**
* 无参数有返回值
*/
interface TestLambda4{
int test();
}
/**
* 一个参数有返回值
*/
interface TestLambda5{
int test(int a);
}
/**
* 两个个参数有返回值
*/
interface TestLambda6{
int test(int a,int b);
}
}
精简语法:
- 参数类型可以省略
- 只有一个参数的情况,()可以省略
- 方法体只有一条语句。{}可以省略
- 如果方法体中的唯一语句是return返回值语句,省略{}的同时return也要省略掉
上述代码简写:
package lambda;
/**
* Lambda语法示例
* DATE = 2023/7/26 22:12
*/
public class Test1 {
public static void main(String[] args) {
// TestLambda1 l1 = ()->{
// System.out.println("无参数无返回值");
// };
TestLambda1 l1 = () -> System.out.println("无参数无返回值");
l1.test();
// TestLambda2 l2 = (int a) -> {
// System.out.println("一个参数无返回值");
// };
TestLambda2 l2 = a -> System.out.println("一个参数无返回值");
l2.test(5);
// TestLambda3 l3 = (int a, int b) -> {
// System.out.println("两个参数无返回值" + (a + b));
// };
TestLambda3 l3 = (a,b) -> System.out.println("两个参数无返回值" + (a + b));
l3.test(4, 7);
// TestLambda4 l4 = () -> {
// return 4;
// };
TestLambda4 l4 = () -> 4;
System.out.println("无参数有返回值" + l4.test());
// TestLambda5 l5 = (int a) -> {
// return a;
// };
TestLambda5 l5 = a -> a;
System.out.println("一个参数有返回值" + l5.test(5));
// TestLambda6 l6 = (int a, int b) -> {
// return a + b;
// };
TestLambda6 l6 = (a,b) -> a + b;
System.out.println("两个参数有返回值" + l6.test(7, 8));
}
/**
* 无参数无返回值
*/
interface TestLambda1 {
void test();
}
/**
* 一个参数无返回值
*/
interface TestLambda2 {
void test(int a);
}
/**
* 一个参数无返回值
*/
interface TestLambda3 {
void test(int a, int b);
}
/**
* 无参数有返回值
*/
interface TestLambda4 {
int test();
}
/**
* 一个参数有返回值
*/
interface TestLambda5 {
int test(int a);
}
/**
* 两个个参数有返回值
*/
interface TestLambda6 {
int test(int a, int b);
}
}
二.方法引用
方法引用是一种简化Lambda表达式的语法形式,它可以直接引用已经存在的方法作为Lambda表达式的实现。通过方法引用,可以将方法作为参数传递给其他方法或函数式接口,从而实现代码的简洁性和可读性。
方法引用可以分为以下几种形式:
-
静态方法引用:引用静态方法,使用类名或者接口名来引用静态方法。 示例:
ClassName::staticMethodName
-
实例方法引用:引用实例方法,使用对象名来引用实例方法。 示例:
objectName::instanceMethodName
-
对象方法引用:引用特定对象的实例方法,使用特定对象的引用来引用实例方法。 示例:
ClassName::instanceMethodName
-
构造方法引用:引用构造方法,使用类名来引用构造方法。 示例:
ClassName::new
方法引用的使用可以简化Lambda表达式的书写,提高代码的可读性和可维护性。它适用于需要传递函数式接口的场景,可以将已经存在的方法直接引用,避免了重复编写Lambda表达式的代码。
下面几种引用的代码示例:
package lambda;
/**
* Lambda语法示例
* DATE = 2023/7/26 22:12
*/
public class Test2 {
public static void main(String[] args) {
//方法引用
Test2 t = new Test2();
TestLambda lambda1 = t::testA;
System.out.println(lambda1.test(6));
//正常Lambda表达式
TestLambda lambda2 = a-> a-2;
System.out.println(lambda2.test(7));
//静态方法引用
TestLambda lambda3 = Test2::testB;
System.out.println(lambda3.test(10));
}
public int testA(int a){
return a-2;
}
public static int testB(int a){
return a-2;
}
/**
* 一个参数有返回值
*/
interface TestLambda {
int test(int a);
}
}
package lambda;
/**
* Author = bianmy
* DATE = 2023/7/26 23:11
*/
public class Dog {
private String name;
private int age;
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;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public Dog() {
System.out.println("无参构造");
}
public Dog(String name, int age) {
System.out.println("带参构造");
this.name = name;
this.age = age;
}
}
package lambda;
/**
* Lambda语法示例
* DATE = 2023/7/26 22:12
*/
public class Test3 {
public static void main(String[] args) {
// Dog1 dog1 = () ->{
// return new Dog();
// };
// System.out.println(dog1.dog());
//构造方法引用
Dog1 dog1 = Dog::new;
System.out.println(dog1.dog());
Dog2 dog2 = Dog::new;
System.out.println(dog2.dog("小黑",30));
}
interface Dog1 {
Dog dog();
}
interface Dog2 {
Dog dog(String name,int age);
}
}