Java:函数式接口

1 概述

函数式接口,是指只有一个抽象方法,但是可以有多个非抽象方法的接口。使用他的目的是为Lambda表达式的使用提供更好的支持,进一步达到函数式编程的目标,提高开发效率。

2 函数式接口的特点

  • 只有一个抽象方法,但是可以有成员、静态和默认方法
  • 使用注解 @FunctionalInterface 可以检查某个接口是否是一个函数式接口(抽象方法的个数是一个)
  • 如果接口是函数式接口,可以添加@FunctionalInterface,也可以不添加
  • 如果某接口包含多余一个抽象方法,在添加@FunctionalInterface 后,会报错

回调函数:简单来说,就是某个方法(A)传入的参数也是一个方法(B),并且在A方法中对方法B进行调用。

3 使用Lambda实现函数式接口

(1)接口的抽象方法没有参数

先定义一个函数式接口

/**
 * 只有一个抽象方法,符合函数式接口的规定 
 */
@FunctionalInterface
public interface FuntionInterface_ {
    
    
	public void laugh();
}

实现:

/**
 * 1.8新写法实现接口
 */
public class FunctionalInterface_03 {
    
    
	public static void main(String[] args) {
    
    
		call (() -> System.out.println("Lambda表达式实现接口")) 
	}
	public static void call(FuntionInterface_ func){
    
    
		func.laugh();
	}
}

(2)接口的抽象方法有参数

定义接口:

@FunctionalInterface
interface MyFunctionInter2 {
    
    
	void printMessage(String str);
}

实现:

public class FunctionalInterface{
    
    
	public static void main(String[] args) {
    
    
		String str = "有参函数式接口调用";
		// 使用 lambda 实现有参函数式接口调用
		// str 是字符串,s 是参数,str 给到 s ,打印 s
		call ( s -> System.out.println(s), str);
	}
	//定义调用接口的回调函数
	public static void call(MyFunctionInter2 func,String str) {
    
    
		func.printMessage(str);
	}
}

4 JDK自带的函数式接口

(1)Supplier : 表示供应商,有返回值,可以获取数据,使用get()方法获取数据。

public class JdkOwn_01 {
    
    
	public static void main(String[] args) {
    
    
	
		String result = getResult(new Supplier<String>() {
    
    
		result = getResult( ()-> "哈哈" );
		System.out.println(result);//哈哈
		
		// 创建Supplier容器
		// 此时不会调用构造方法创建对象
		Supplier<JdkOwn_01> sup = JdkOwn_01::new;
		// 创建对象并返回
		JdkOwn_01 jo1 = sup.get();
		JdkOwn_01 jo2 = sup.get();
		System.out.println(jo1);
		System.out.println(jo2);
	}

	//定义获取方法,传入供应商接口对象,由返回值
	public static String getResult(Supplier<String> function) {
    
    
		//固定写法,通过get() 获取内容
		//具体获取什么需要自己实现
		return function.get();
	}
}

(2) Consumer : 表示消费者接口,不需要返回值,accept(T t) :用于执行消费操作。

public class JdkOwn_02 {
    
    
	public static void main(String[] args) {
    
    
		String message = "我爱花钱";
		//lambda 实现接口
		//message是参数
		//因为该消费接口是需要参数的,需要定义 x ,接受 message
		//然后打印 x
		consumeResult(x -> System.out.println(x), message);
	}
	//定义消费方法,传入消费接口对象和消费的内容
	public static void consumeResult(Consumer<String> function, String message) {
    
    
		function.accept(message);
	}
}

(3)Function<T,R> :接收一个参数,并产生一个结果,T是参数,R是结果,R apply(T t) 方法制造结果。

public class JdkOwn_03 {
    
    
	public static void main(String[] args) {
    
    
		int num = convertType(x->Integer.parseInt(x), "123");
		System.out.println(num);
	}

	/**
 	 * 接受一个字符串,将其转换为数字,数字是返回值
	 * @param function  自定义接口
	 * @param str  字符串,参数
	 * @return  数字,返回值
	 */
	public static int convertType(Function<String, Integer> function, String str) {
    
    
		int num = function.apply(str);
		return num;
	}
}

(4)Predicate : 断言接口,用来做一些判断,booean test(T) :用于做校验比较操作。

public class JdkOwn_04 {
    
    
	public static boolean call(Predicate<String> predicate, String message) {
    
    
		return predicate.test(message);
	}
	public static void main(String[] args) {
    
    
		String message = "ok";
		boolean result = call ( x -> {
    
    return x.equals("ok");}, message);
		System.out.println(result);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41504815/article/details/113575368