浅谈Lambda表达式、引用以及函数式接口

Lambda表达式

表达式要求:SAM(Single Abstract Method),一个接口中只有一个抽象方法,除此之外没有任何其他方法定义,这样的抽象方法成为函数式接口,函数式接口才能为Lambda表达式所使用。

一般在函数式接口前面加上“@FunctionalInterface”表明函数式接口。

Lambda表达式格式:

  1. 方法没有参数:()->{语句};
//实例:
@FunctionalInterface
interface IMessage
{
	public void printClassName();
}
public class Demo6 {

	public static void main(String[] args) {
		IMessage cn = ()->{
			System.out.println("abc");
		};
		cn.printClassName();
	}
}
输出结果:abc
interface IAdd{
	public int plus(int x,int y);

}
class Add{
	public static int add(int x,int y){
		return x+y;
	}
}
public class Demo{
	public static void main(String[] args){
		IAdd a = Add :: add;
		System.out.println(a.plus(1,2));
	}
}
输出结果:3
  1. 方法有参数:(参数1,参数2)->{语句};
//实例:
@FunctionalInterface
interface IMessage
{
	public void printClassName(String str);
}
public class Demo6 {

	public static void main(String[] args) {
		IMessage cn = (str)->{
			System.out.println("abc"+str);
		};
		cn.printClassName("Bullet");
	}
}
输出结果:abcBullet
  1. 如果只有一行语句并且是返回语句:(参数1,参数2)->语句;
//实例:
@FunctionalInterface
interface IMessage
{
	public String printClassName(String str);
}
public class Demo6 {

	public static void main(String[] args) {
		IMessage cn = (str)-> "abc"+str ;     
		//不需要加大括号也不需要加return
		System.out.println(cn.printClassName("Bullet"));
	}
}
输出结果:abcBullet

引用

引用即 让不同的方法名称可以描述同一个方法

接口引用要求引用类必须是函数式接口!

引用格式:

1. 引用静态方法:类名称::Static方法名称;

++必须是函数式接口(SAM标准)++

//实例: String.valueOf();
//方法定义:public static String valueOf(int i);     -该方法有参数和返回值

@FunctionalInterface
interface IFunction<P,R>{      //泛型类型,参数P,返回值R
	public R change(P p);
}
public class Demo6 {

	public static void main(String[] args) {
		IFunction<Integer,String> ch = String :: valueOf;
		//将String.valueOf取别名ch.change
		String str = ch.change(999);
		System.out.println(str.length());
	}
}

输出结果:3
2. 引用某个实例对象的方法:实例化对象:: 普通方法;

++必须是函数式接口(SAM标准)++

++有实例化对象++

//实例方法定义:public String toUpperCase();

@FunctionalInterface
interface IFunction<R>{
	public R upper();
}
public class Demo6 {

	public static void main(String[] args) {
		IFunction<String> up = "www.sdibt.edu.cn" :: toUpperCase;
		//有实例化对象"www.sdibt.edu.cn" 
		String str = up.upper();
		System.out.println(str);
	}
}
输出结果:WWW.SDIBT.EDU.CN
interface IAdd{
	public int plus(int x,int y);

}
class Add{
	public int add(int x,int y){
		return x+y;
	}
}
public class Demo{
	public static void main(String[] args){
		Add q = new Add();
		IAdd a = q :: add;
		System.out.println(a.plus(1,2));
	}
}
输出结果:3
3. 引用特定类型的方法:特定类::普通方法;

++必须是函数式接口(SAM标准)++

++没有实例化对象,只想进行方法引用,可使用特定类处理++

//实例方法定义:public int compareTo(String anotherString);
@FunctionalInterface
interface IFunction<P>{
	public int compare(P p1,P p2);
}
public class Demo6 {

	public static void main(String[] args) {
		IFunction<String> fun = String :: compareTo;
		System.out.println(fun.compare("A", "a"));
	}
}
输出结果:-32
4. 引用构造方法:类名称::new;
@FunctionalInterface
interface IFunction<R>{
	public R create(String s,int a);
}
class Person {
	private String name;
	private int age;
	
	public Person(String s,int a) {
		this.name = s;
		this.age = a;
	}
	public String toString() {
		return "Name:"+name+"、age:"+age;
	}
}
public class Demo6 {

	public static void main(String[] args) {
		IFunction<Person> fun = Person :: new;
		System.out.println(fun.create("张三", 20));
	}
}
输出结果:Name:张三、age:20

预置函数式接口

系统中有预置函数式接口,存在java.util.function包中,有如下核心接口供使用:

不要忘记导入包:

import java.util.function.*;
1.功能型函数式接口(有接收值有返回值):
@FunctionalInterface
public interface Function<T,R>{
    public R apply(T t);
}
//实例:
import java.util.function.*;
class Add{
	public String add(String a){
		return a.toUpperCase();              //作用将字符串变大写
	}
}
public class Demo{
	public static void main(String[] args){
		Add q = new Add();
		Function <String,String> fun = q :: add;     //<接收值,返回值>
		System.out.println(fun.apply("Hello!"));
	}
}
2.消费型函数式接口(只有接收值,没有返回值):
@FunctionalInterface
public interface Consumer<T>{
    public void accept(T t);
}
//实例:
import java.util.function.*;
class Add{
	public void add(String a){
		System.out.println(a);
	}
}
public class Demo{
	public static void main(String[] args){
		Add q = new Add();
		Consumer <String> fun = q :: add;
		fun.accept("hello!");
	}
}
3.供给型函数式接口(没有接收值,只有返回值):
@FunctionalInterface
public interface Supplier<T>{
    public T get();
}
//实例:
import java.util.function.*;
class Add{
	public String add(){
		return "Hello World!";
	}
}
public class Demo{
	public static void main(String[] args){
		Add q = new Add();
		Supplier <String> fun = q :: add;
		System.out.println(fun.get());
	}
}
4.断言型函数式接口(有接收值,但是返回值一定是boolean):
@FunctionalInterface
public interface Predicate<T>{
    public boolean test(T t);
}
//实例:
import java.util.function.*;
class Add{
	public String add(){
		return "Hello World!";
	}
}
public class Demo{
	public static void main(String[] args){
		Add q = new Add();
		Predicate <String> fun = "sdibt" :: equalsIgnoreCase;
		System.out.println(fun.test("SDIBT"));
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值