【Java脱坑指南】 lambda表达式

/**
 * Lambda expressions basically express instances of functional interfaces (An interface 
 * with single abstract method is called functional interface. An example is java.lang.Runnable). 
 * lambda expressions implement the only abstract function and therefore implement functional interfaces
 * 
 * lambda expressions are added in Java 8 and provide below functionalities.
 *     · Enable to treat functionality as a method argument, or code as data.
 *     · A function that can be created without belonging to any class.
 *     · A lambda expression can be passed around as if it was an object and executed on demand.
 * lambda 表达式通常是“函数式接口”的一个实例。(只包含一个抽象方法的的接口称为 函数式接口,例如 java.lang.Runnable 只包含一个 run 方法)
 * lambda 表达式实现了唯一的抽象方法,因此也实现了函数式接口
 * lambda 表达式在 java8 添加并提供了以下功能:
 *		· 可以将方法当成参数传递
 *		· 可以创建不属于任何类的方法
 *		· lambda表达式可以像传递对象一样传递,并按需执行。
 */
public class LambdaTest {
	
	// 声明为函数式接口,不允许添加新的接口函数
	@FunctionalInterface	
	interface FuncInterfaceA {
		int single(int x);
	}
	
	@FunctionalInterface	
	interface FuncInterfaceB {
		int add(int x, int y);
	}
	
	public static void main(String[] args) {
		FuncInterfaceA fa = x -> x; // 一个参数时可以省去括号
		System.out.println("x = " + fa.single(11));
		
		FuncInterfaceB fb = (x, y) -> x + y;
		System.out.println("x + y = " + fb.add( 11, 22 ));
		
		// 优雅的开启线程
		new Thread( () -> {} )  .start();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值