jdk 8 函数接口之function

JKD8 学习

什么叫函数接口?
  1. 有且只能有一个抽象方法
  2. 加了@functionalInterface注解
  3. 不加funcationalInterface 主要满足只有一个抽象方法也会被jdk8认为是函数式接口
function接口
@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
  
   default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }


    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }
  • R apply(T t);
    只接受一个参数,然后返回一个参数

  • compose 方法

default Function<V, R> compose(Function<? super V, ? extends T> before) 方法 先运行传入的这个函数方法然后运行函数本身

  • andThen

default Function<T, V> andThen(Function<? super R, ? extends V> after) 方法是先执行函数本身然后在执行传入的function 函数 正好与compose 的方法相反

代码示例如下:

public class FunctionTest {

	public static void main(String[] args) {
		FunctionTest test1 = new FunctionTest();
		System.out.println(test1.compute1(2, value-> value * 3, value -> value * value));
		System.out.println(test1.compute2(2, value-> value * 3, value -> value * value));
	}

	
	
	public int compute1(Integer a, Function<Integer,Integer> func1,Function<Integer, Integer> func2) {
		return func1.compose(func2).apply(a);
		
	}
	public int compute2(Integer a, Function<Integer,Integer> func1,Function<Integer, Integer> func2) {
		return func1.andThen(func2).apply(a);
		
	}
}


12
36
  • Bifuncation
    可以传俩个参数
	public int compute3(Integer a, Integer b,BiFunction<Integer,Integer,Integer> func2) {
		return func2.apply(a, b);
		
	}
	System.out.println(test1.compute3(2,  3, (value,value2) -> value * value2));

Bifuncation 只有一个andThen 方法 没有compose 方法 因为compose 方法是先运行传入函数本身 运行完之后就是一个值了.
那么这样的一个方法就和function 的compose 方法是一样了.
他也无法调用Bifunction 的apply 方法了因为 这个方法需要俩个入参
那么Bifunction 为什么会有andThen呢? 因为andthen方法是先调用函数本身的apply(r,u,t) 调用完之后就变成一个值 可以让入参的function函数调用

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JDK8中,引入了许多函数接口。其中一些常用的函数接口包括: 1. Function接口:该接口表示一个函数,接受一个参数并产生一个结果。它定义了一个名为apply的抽象方法,用于执行函数的逻辑。 2. Consumer接口:该接口表示一个消费者,接受一个参数并执行某些操作,但没有返回值。它定义了一个名为accept的抽象方法,用于执行消费者的逻辑。 3. Supplier接口:该接口表示一个供应商,不接受任何参数,但产生一个结果。它定义了一个名为get的抽象方法,用于获取供应商的结果。 4. Predicate接口:该接口表示一个断言,接受一个参数并返回一个布尔值。它定义了一个名为test的抽象方法,用于执行断言的逻辑。 这些函数接口可以与Lambda表达式一起使用,以实现更简洁和灵活的代码编写。通过使用这些函数接口,可以将行为作为参数传递给方法,或者在需要时创建匿名函数。 请注意,这只是一些常用的函数接口JDK8还提供了其他函数接口,如BiFunction、BiConsumer、BiPredicate等,以满足不同的编程需求。 #### 引用[.reference_title] - *1* *2* *3* [jdk1.8之函数接口](https://blog.csdn.net/liu_shi_jun/article/details/128562977)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值