JDK1.8学习笔记2-函数式接口

本文详细介绍了JDK1.8中的函数式接口,包括基本用法、compose和andThen方法的运用,双参数和断言函数式接口的特性,并通过示例展示了各种接口的实际操作和结果。
摘要由CSDN通过智能技术生成

1Function函数式接口

1.1基本用法

抽象接口

  • apply—将此函数应用于给定的参数

默认实现接口

  • compose—将compose传入函数的返参作为当前函数的入参
  • andThen—将当前函数的返参作为andThen的入参

1.2 compose方法

public class FunctionTest {
   
    public static void main(String[] args) {
   
        FunctionTest functionTest = new FunctionTest();

        System.out.println(functionTest.compute(1, value ->  2 * value));
        System.out.println(functionTest.compute(2, value -> 5 + value));
        System.out.println(functionTest.compute(3, value -> value * value));
        System.out.println(functionTest.convert(4, value -> value + " convert"));
        //
  		Function<Integer, Integer> function = value -> 2 * value;
        System.out.println(functionTest.compute(1, function));
    }

    public int compute(int a, Function<Integer, Integer> function){
   
        int result = function.apply(a);
        return result;
    }

    public String convert(int a, Function<Integer, String> function){
   
        return function.apply(a);
    }
}

高阶函数:—接收一个函数作为参数,或者将一个函数作为返回值的函数

1.3 compose方法

示例

public class FunctionTest2 {
   
    public int composeTest(int a, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
   
        return function1.compose(function2).apply(a);
    }

    public static void main(String[] args) {
   
        FunctionTest2 functionTest2 = new FunctionTest2();
        System.out.println(functionTest2.composeTest
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值