Lambda表达式举例
1.TreeSet集合排序时候,使用自然排序,需要元素实现Comparable接口.
匿名内部类方式:
Comparable<Integer> comparable = new Comparable<Integer>() {
@Override
public int compareTo(Integer o) {
return 0;
}
};
Lambda表示方式:
Comparable<Integer> comparable1 = o -> 0;
2.比较器排序,实现Comparator接口,重写compare方法
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return 0;
}
};
Comparator<Integer> comparator = (o1, o2) -> 0;
3.线程中的Runnable任务接口
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("子线程执行了!");
}
};
Runnable runnable1 = () -> System.out.println("子线程执行了!");
4.线程中的Callable任务接口
Callable<Integer> callable = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
System.out.println("子线程执行了,返回100");
return 100;
}
};
Callable<Integer> callable = () -> {
System.out.println("子线程执行了,返回100");
return 100;
};
5.反射中的动态代理接口
InvocationHandler invocationHandler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object invoke = method.invoke(new Object());
return invoke;
}
};
InvocationHandler invocationHandler = (proxy, method, args1) -> {
Object invoke = method.invoke(new Object());
return invoke;
};
Lambda书写方法和注意事项
好了,举例了这么多,现在说说怎么写:
Lambda表达式用->箭头分为两部分,简写匿名内部类,左边写要实现接口中的抽象方法的参数(参数名随便写x,y,z任意都行),右边写具体实现逻辑
如果抽象方法没有参数,左边写一个()就行
如果抽象方法只有一个参数则可以省略括号,直接写一个参数名
如果抽象方法有多个参数,需要全部写
如果可以根据泛型,或者抽象方法中额参数类型推断出参数的数据类型则括号中不用写参数类型,如果不能推断出来则默认为Object类型
如果右边具体代码逻辑实现只有一行则不用写{},直接写在->后面
如果右边具体代码逻辑实现只有一行且为返回值则不用写{},直接写在->后面所要返回的值,不用写return
如果右边具体代码逻辑实现只有不止一行,则需要加上{},全部写上
注意不是所有接口都可以用Lambda表达式写出来,
只有函数接口(只有一个抽象方法)才可以写出来,要不然不知道Lambda表达的是里面的那个抽象方法.
@FunctionalInterface原码上会有这个检测函数式接口的标记.有些抽象类没有这个标记也能写出Lambda表达式.
深层简写:
若你对一个接口的逻辑实现,这个抽象方法的参数和返回值和你要用的另一个方法的一致,就可以再次简写
例如Callable接口需要一个返回值为类型Integer.
例1—类名调用静态方法
匿名内部类:
Supplier<Double> supplier = new Supplier<Double>() {
@Override
public Double get() {
return Math.random();
}
};
简写1:
Supplier<Double> supplier1 = () -> Math.random();
Double double1 = supplier1.get();
简写2:
Supplier<Double> supplier1 = Math::random;
Double double1 = supplier1.get();
例2
Function<Integer, String> function = new Function<Integer, String>() {
@Override
public String apply(Integer integer) {
return String.valueOf(integer);
}
};
Function<Integer, String> function = integer -> String.valueOf(integer);
Function<Integer, String> function = String::valueOf;
例3
BiFunction<Integer, Integer, Integer> biFunction = new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) {
return Math.max(integer, integer2);
}
};
BiFunction<Integer, Integer, Integer> biFunction = (integer, integer2) -> Math.max(integer, integer2);
BiFunction<Integer, Integer, Integer> biFunction = Math::max;
例4----对象调用实例方法
System.out是个PrintStream对象
Consumer<String> consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
Consumer<String> consumer1 = s -> System.out.println(s);
Consumer<String> consumer1 = System.out::println;
PrintStream out1 = System.out;
Consumer<String> consumer1 = out1::println;
例5----参数调用
Comparator<String> comparator1 = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
};
Comparator<String> comparator2 = (s1, s2) -> s1.compareTo(s2);
Comparator<String> comparator2 = String::compareTo;
例6----构造调用
class Student{
public String name;
public int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
空参构造引用
Supplier<Student> student1 = new Supplier<Student>() {
@Override
public Student get() {
return new Student();
}
};
Supplier<Student> student1 = () -> new Student();
Supplier<Student> student1 = Student::new;
有参构造引用
Supplier<Student> student2 = new Supplier<Student>() {
@Override
public Student get() {
return new Student("bob", 20);
}
};
Supplier<Student> student2 = () -> new Student("bob", 20);
//有参不能再次简写,但可以写个静态方法,自己调用
Supplier<Student> student2 = Demo2::get;//调用自己写的静态方法
//写一个下面的静态方法
private static Student get() {
return new Student("bob", 20);
}
函数式接口
其实我们的Lambda表达式就是对函数式接口的一种简写方式,所以只有是函数式接口,我们才能用Lambda表达式.再换句话说,Lambda表达式需要函数式接口的支持,那函数式接口我们可以自己定义,当然JDK1.8也给我们提供了一些现成的函数式接口.
函数式接口的定义是: 只包含一个抽象方法的接口,称为函数式接口。
你可以通过 Lambda 表达式来创建该接口的对象
我们可以在任意函数式接口上使用 @FunctionalInterface 注解,这样做可以检查它是否是一个函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口.
谢谢!