Java8的一个新特性:Lamba表达式
- Lambda 表达式是一种匿名函数
- Lambda 表达式没有声明的方法,也没有访问修饰符、返回值声明和名字
- 当某个方法只使用一次,而且定义很简短,使用lamba替代之尤其有效,这样,你就不必在类中费力写声明与方法了
Lambda 表达式
Lambda 表达式通常使用 (argument) -> (body)
(type1 arg1, type2 arg2...) -> { body }
例子:
(int a, int b) -> { return a + b; }
() -> System.out.println("Hello World");
(String name) -> { System.out.println("欢迎"+name+"来到wingzingCSDN"); }
() -> { return 3.1415 };
Lambda 表达式的结构
- 一个 Lambda 表达式可以有零个或多个参数
- 参数的类型既可以明确声明,也可以根据上下文来推断。例如:
(int a)
与(a)
效果相同 - 所有参数需包含在圆括号内,参数之间用逗号相隔。例如:
(a, b)
或(int a, int b)
或(String a, int b, float c)
- 空圆括号代表参数集为空。例如:
() -> 42
- 当只有一个参数,且其类型可推导时,圆括号()可省略。例如:
a -> return a*a
- Lambda 表达式的主体可包含零条或多条语句
- 如果 Lambda 表达式的主体只有一条语句,花括号{}可省略。匿名函数的返回类型与该主体表达式一致
- 如果 Lambda 表达式的主体包含一条以上语句,则表达式必须包含在花括号{}中(形成代码块)。匿名函数的返回类型与代码块的返回类型一致,若没有返回则为空
一些 Lambda 表达式及其函数式接口:
Consumer<Integer> c = (int x) -> { System.out.println(x) };
BiConsumer<Integer, String> b = (Integer x, String y) -> System.out.println(x + " : " + y);
Predicate<String> p = (String s) -> { s == null };
函数式接口
函数式接口是只包含一个抽象方法声明的接口。
java.lang.Runnable
就是一种函数式接口,在 Runnable 接口中只声明了一个方法 void run()
,相似地,ActionListener 接口也是一种函数式接口,我们使用匿名内部类来实例化函数式接口的对象,有了 Lambda 表达式,这一方式可以得到简化。
函数式接口只能有一个抽象方法。
@FunctionalInterface
public interface WorkerInterface {
public void doSomeWork();
// public void doSomeMoreWork(); 当添加多个抽象方法的时候,编译器会报错
}
函数接口和lambda表达式的用法
@FunctionalInterface
public interface WorkerInterface {
public void doSomeWork();
}
public class WorkerInterfaceTest {
public static void execute(WorkerInterface worker) {
worker.doSomeWork();
}
public static void main(String [] args) {
//常规调用方法
execute(new WorkerInterface() {
@Override
public void doSomeWork() {
System.out.println("doSomeWork");
}
});
//Lambda表达式调用方法
execute( () -> System.out.println("doSomeWork by lambda") );
}
}
Demo
public class wingzingDemo{
public static void main(String [] a) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
System.out.println("Print all numbers:");
evaluate(list, (n)->true);
System.out.println("Print no numbers:");
evaluate(list, (n)->false);
System.out.println("Print even numbers:");
evaluate(list, (n)-> n%2 == 0 );
System.out.println("Print odd numbers:");
evaluate(list, (n)-> n%2 == 1 );
System.out.println("Print numbers greater than 5:");
evaluate(list, (n)-> n > 5 );
}
public static void evaluate(List<Integer> list, Predicate<Integer> predicate) {
for(Integer n: list) {
if(predicate.test(n)) {
System.out.println(n + " ");
}
}
}
}
Print all numbers: 1 2 3 4 5 6 7
Print no numbers:
Print even numbers: 2 4 6
Print odd numbers: 1 3 5 7
Print numbers greater than 5: 6 7
Predicate
延伸出来Predicate,Predicate也是java8的新特性,可以应用于lambda表达式。
我写这篇文章之前也没有接触过这些,阅读源码发现其实predicate并没有很复杂。
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
即对t进行断言,返回true或者false,后面的n%2 == 0,n%2 == 1 都表示条件。同样的也可以使用正则表达式作为参数。
后言
个人认为,对于Lambda表达式等类型的技术,进行一定的了解就可以然后能读得懂就行。因为这种表达式写起来方便但是实际上会提升后面别人看代码的阅读难度,类似于三元运算符等等,并不是特别推荐使用,以上。