JAVA8 新特性 Lambda 表达式 / 方法引用

Lambda 表达式

匿名类定义语法

首先需要掌握匿名类的使用规则,可点击本链接了解

表达式定义条件 必备一个【函数式接口】

必须定义一个有且仅有一个抽象方法的接口

interface MyInter{
	int num(int n);
}

接口的匿名类实现方式

MyInter myInter = new MyInter() {
	@Override
	public int num(int n) {
		// TODO Auto-generated method stub
		return 0;
	}
};	

表达式实现方式一 :

语法: (参数列表)->{方法体};

MyInter myInter = (n) -> { return 0; };

表达式实现方式二:

如果参数列表中只有一个参数,可省略()括号

MyInter myInter = n -> { return 0; };

表达式实现方式三:

如果方法体只有一条语句且没有返回值,可省略 { } 括号
如果方法体只有一条语句且有返回值时,可将 { } 与 return 语句 一起省略

MyInter myInter = n -> 0;

方法引用

方法引用是用来直接访问类对象实例已经存在的【方法/属性】,
然后将【方法/属性】绑定到另一对象的【方法/属性】上。
语法:方法引用使用一对冒号 :: 。

引用的实现方式:

package learn.Lambda;

interface MyInter {
	int num(int n);
}

public class Demo {

	public static void main(String[] args) {
		
		// 将Demo对象的 getInt 方法绑定到 obj.num 方法上
		// 绑定时两个方法【形参,返回值】必须要求一致
		MyInter obj = new Demo()::getInt;
		int res = obj.num(250);

		System.out.println(res);

	}
	
	public int getInt(int n) {
		return n;
	}
}

引用的更多实现方式:

package learn.Lambda;

interface MyInter {
	int num(int n);
}

public class Demo {
	
	static Demo staticProp = new Demo();

	public static Demo staticFun() {
		return new Demo();
	}

	public int getInt(int n) {
		return n;
	}

	public static int staticGetInt(int n) {
		return n;
	}
	
	public static void main(String[] args) {
		
		MyInter obj1 = new Demo()::getInt;
		int res1 = obj1.num(250);

		MyInter obj2 = Demo.staticFun()::getInt;
		int res2 = obj2.num(520);

		MyInter obj3 = Demo.staticProp::getInt;
		int res3 = obj3.num(1314);

		MyInter obj4 = Demo::staticGetInt;
		int res4 = obj4.num(380);


		System.out.println(res1 + "-" + res2 + "-" + res3 + "-" + res4);// 250-520-1314-380

	}
}

常用函数式接口

由JAVA官方定义的,类似于上文中的 MyInter

1. Consumer【消费型接口】

在内部对这个数据进行处理,不需要返回任何的数据
接口抽象方法:void accept(T t);

package learn.Lambda;

import java.util.function.Consumer;

public class Demo1 {

	public static void main(String[] args) {
		
		Consumer<String> consumer = str -> {
			System.out.println(str);
		};
		
		consumer.accept("消费型接口,在内部对这个数据进行了处理,不需要返回任何的数据");

		// 方法引用写法
		Consumer<String> consumer1 = System.out::println;
		consumer1.accept("System.out 为一个对象,将此对象的 println 方法绑定到consumer1的accrpt方法上,调用效果同上");
	}

}

2. Supplier【供给型接口】

可以提供一个数据
接口抽象方法: T get();

package learn.Lambda;

import java.util.function.Supplier;

public class Demo2 {

	static String echoHello() {
		return "echoHello";
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

//		Supplier<String> supplier = () -> {
//			return "哈喽";
//		};

		Supplier<String> supplier = () -> "哈喽";
		System.out.println(supplier.get());// 哈喽
		
		// 方法引用写法
		Supplier<String> supplier1 = Demo2::echoHello;
		System.out.println(supplier1.get());// echoHello
		
	}

}

3. Function【函数型接口】

接收一个参数 返回一个数据
接口抽象方法: R apply (T t)

package learn.Lambda;

import java.util.function.Function;

public class Demo3 {
	
	static Integer count(int n) {
		return n * 5;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub


//		Function<Integer, Integer> function = param -> {
//			return param + 1;
//		};
		
		Function<Integer, Integer> function = param -> param + 1;
		System.out.println(function.apply(5));// 6
		
		// 方法引用写法
		Function<Integer, Integer> function1 = Demo3::count;
		System.out.println(function1.apply(5));// 25
	}

}

4. Predicate【断言型接口】

判断一个对象是否符合某个条件
接口抽象方法: boolean test(T t)

package learn.Lambda;

import java.util.function.Predicate;

public class Demo4 {
	
	static boolean condition(String str) {
		return str.length() <= 5;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

//		Predicate<String> predicate = param -> {
//			return param.length() > 5;
//		};
		
		Predicate<String> predicate = param -> param.length() > 5;
		System.out.println(predicate.test("我自横刀向天笑,笑完我就去睡觉!"));// true
		
		// 方法引用写法
		Predicate<String> predicate1 = Demo4::condition;
		System.out.println(predicate1.test("我自横刀向天笑,笑完我就去睡觉!"));// false
	}
	
}

以上是整理的一些JAVA8中的表达式和方法引用的新语法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值