前言
Lambda表达式是Java8新特性之一,平时我们在写代码时,就可以用到Lambda来简化我们的代码,俗话说菜鸟10行代码大佬只需一行代码解决,其不含就是简化了代码,下面就给大家介绍Lambda的几大用法。
1、介绍
- 匿名函数 - 一段可以传递的代码,将代码像数据一样传递 - 语法糖 - Lambda的本质: 作为函数式接口的实例 - Lambda表达式只适用于函数式接口
2、关键词
-
@FunctionalInterface 一个接口中只有一个抽象方法
-
java.util.function包中全部是函数式接口
3、匿名函数实现
3.1 无参,无返回值,单条语句
// 无参,无返回值
interface IMethod {
void say();
}
@Test
public void test1() {
// 省去{}
IMethod iMethod = () -> System.out.println("你好");
iMethod.say();
}
3.2 一参,无返回值,单条语句
// 有参(一个参数,小括号可省略),无返回值
interface IMethod {
void say(String name);
}
@Test
public void test1() {
// 省去{} ,省去()
IMethod iMethod = name -> System.out.println("你好,"+name);
iMethod.say("金大大");
}
3.3 多参,无返回值,单条语句
// 有参,无返回值
interface IMethod {
void say(String name, int age);
}
@Test
public void test1() {
// 省去{},()不可省去
IMethod iMethod = (name, age) -> System.out.println("你好," + name + age);
iMethod.say("金大大", 18);
}
3.4 多参,有返回值,单条语句
interface IMethod {
int sum(int a, int b);
}
@Test
public void test1() {
// 省去return,省去{}
IMethod iMethod = (a, b) -> a + b;
System.out.println(iMethod.sum(1, 2));
}
3.5 多参数,有返回值,多条语句
interface IMethod {
int sum(int a, int b);
}
@Test
public void test1() {
// {}不可省略, return不可省略
IMethod iMethod =
(a, b) -> {
System.out.println("让我想想" + a + "+" + b + "=多少呢?");
return a + b;
};
System.out.println(iMethod.sum(1, 2));
}
4、方法引用
4.1 对象 : : 非静态方法
-
案例一
// 原始写法
Consumer<String> consumer1 = str -> System.out.println(str);
consumer1.accept("200软妹币");
// ================================================================
// 一个对象
PrintStream printStream = System.out;
// 使用 对象::非静态方法
Consumer<String> consumer2 = printStream::println;
consumer2.accept("200软妹币");
// 原理:
// Consumer中的 void accept();
// PrintStream中的 void println();
-
案例二
class MySupplier {
public String getSomething() {
return "提供200软妹币";
}
}
public void test4() {
// 完整写法
Supplier<String> stringSupplier1 =
new Supplier<String>() {
@Override
public String get() {
return "提供200软妹币";
}
};
// Lambda简写
Supplier<String> stringSupplier2 = () -> "提供200软妹币";
// 使用方法引用
MySupplier r = new MySupplier();
// Supplier<String> stringSupplier3 =()-> r.getSomething();
Supplier<String> stringSupplier3 = r::getSomething;
System.out.println(stringSupplier3.get());
// 原理:
// Supplier中的 T get()
// MySupplier中的 String getSomething()
}
4.2 类 : : 静态方法
-
案例一
// 一个接口
static interface IMethod {
int sum(int a, int b);
}
// 另一个类的add方法参数列表恰好吻合,且返回值类型吻合
static class OtherMethod {
public static int add(int a, int b) {
return a + b;
}
}
@Test
public void test1() {
// 方法引用 (用别人的方法去实现自己的方法)
IMethod iMethod = OtherMethod::add;
System.out.println(iMethod.sum(1, 2));
}
-
案例二
Comparator<Integer> comparator1 = (t1, t2) -> Integer.compare(t1, t2);
System.out.println(comparator1.compare(1, 2));
Comparator<Integer> comparator2 = Integer::compare;
System.out.println(comparator2.compare(1, 2));
// 原理:
// Comparator中的 int compare(T t1,T t2)
// Integer int compare(T t1,T t2)
-
案例三
Function<Double, Long> function1 = d -> Math.round(d);
System.out.println(function1.apply(200.00));
Function<Double, Long> function2 = Math::round;
System.out.println(function2.apply(200.00));
// 原理:
// Function中 R apply(T d)
// Math中 Long round(Double d)
4.3 类 : : 非静态方法(实例方法)
-
案例一
Comparator<String> comparator1 = (s1, s2) -> s1.compareTo(s2);
Comparator<String> comparator2 = String::compareTo;
System.out.println(comparator2.compare("12", "123"));
// 原理:
// Comparator中的 int compare(T t1,T t2)
// String中 int t1.compareTo(t2) 方法
-
案例二:
BiPredicate<String, String> biPredicate1 = (s1, s2) -> s1.equals(s2);
BiPredicate<String, String> biPredicate2 = String::equals;
System.out.println(biPredicate2.test("123", "123"));
-
案例三
class User {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
@Test
public void test5() {
Function<User, String> function1 = user -> user.getName();
Function<User, String> function2 = User::getName;
User user = new User();
user.setName("jdd");
System.out.println(function2.apply(user));
}
5、构造器引用
5.1 案例一
Supplier<User> supplier1 = () -> new User();
Supplier<User> supplier2 = User::new;
System.out.println(supplier2.get());
5.2 案例二
Function<String, User> function1 = name -> new User(name);
System.out.println(function1.apply("jdd"));
Function<String, User> function2 = User::new;
System.out.println(function2.apply("jdd"));
5.3 案例三
Function<Integer, String[]> function1 = length -> new String[length];
Function<Integer, String[]> function2 = String[]::new;
System.out.println(Arrays.toString(function2.apply(4)));
总结:在写代码时,大家可以使用Lambda表达式来写,不但可以提高效率,代码也会变得高大尚哦,但是有一点不得不说,写出来的代码可读性很差。
关注成都知了堂,获得更多学习资料:https://zhiliaotang.cn