掌握JDK8:(三)函数式接口Function接口详解

JDK8开始,Java在java.util.function包里面加入了很多的函数式接口。见下图。

今天我们先讲一下Function接口。

package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts one argument and produces a result.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 *
 * @param <T> the type of the input to the function
 * @param <R> the type of the result of the function
 *
 * @since 1.8
 */
@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);

    /**
     * Returns a composed function that first applies the {@code before}
     * function to its input, and then applies this function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of input to the {@code before} function, and to the
     *           composed function
     * @param before the function to apply before this function is applied
     * @return a composed function that first applies the {@code before}
     * function and then applies this function
     * @throws NullPointerException if before is null
     *
     * @see #andThen(Function)
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    /**
     * Returns a composed function that first applies this function to
     * its input, and then applies the {@code after} function to the result.
     * If evaluation of either function throws an exception, it is relayed to
     * the caller of the composed function.
     *
     * @param <V> the type of output of the {@code after} function, and of the
     *           composed function
     * @param after the function to apply after this function is applied
     * @return a composed function that first applies this function and then
     * applies the {@code after} function
     * @throws NullPointerException if after is null
     *
     * @see #compose(Function)
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    /**
     * Returns a function that always returns its input argument.
     *
     * @param <T> the type of the input and output objects to the function
     * @return a function that always returns its input argument
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

定义:Function接口代表接受一个参数,并放回一个结果的函数。

有两个范型

  • T 代表传入的参数类型,
  • R 表示返回的结果类型。

Function接口是一个函数式接口,他的函数式方法是apply

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

举个例子吧,更直观。
 

import java.util.function.Function;

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

        //我想计算3的平方
        //传统方式定义function对象
        Function<Integer, Integer> function = new Function<Integer, Integer>() {

            @Override
            public Integer apply(Integer integer) {
                return integer * integer;
            }
        };
        int result = functionTest.compute(3, function);
        System.out.println("我想计算3的平方 = [" + result + "]");


        //我考了89分,还差多少分满分。
        //lambda方式 定义function
        Function<Integer, Integer> function1 = (param) -> {
            return 100 - param;
        };
        int result1 = functionTest.compute(89, function1);
        System.out.println("我考了89分,还差多少分满分 = [" + result1 + "]");


        //我想计算3*2+8-3:
        int result2 = functionTest.compute(3, (actionParam) -> {
            return actionParam * 2 + 8 - 3;
        });
        System.out.println("我想计算3*2+8-3: = [" + result2 + "]");

        //我想计算1M等于多少k
        //简化版本lambda方式  如果参数是一个的话,参数的()可以省略,如果方法体也就一句的话,也可以省略{}.
        int result3 = functionTest.compute(1, actionParam -> actionParam * 1024);
        System.out.println("我想计算1M等于多少k = [" + result3 + "]");
    }

    /**
     * 代表一类计算,传入int类型,计算后还返回int类型的计算。
     *
     * @param param    传入的参数,int类型的。
     * @param function 计算行为,计算的行为。怎么计算,方法调用者说了算。
     * @return 结果,int类型的。
     */
    private int compute(int param, Function<Integer, Integer> function) {
        return function.apply(param);
    }
}

例子中定义一个compute方法。compute方法是接受两个参数,一个int类型的param,和一个Function的参数,其中Function定义了传入输入、输出都是Integer类型的。

main方法中完成了4个实例计算。

  1. 3的平方
  2. 差多少满分
  3. param*2+8-3的计算
  4. 1M等于多少k

没错,这四种完全不同的计算行为,一个compute方法都搞定。

怎么搞定的呢?

没错,就是function参数搞定的。

function参数定义了 输入参数:Integer  返回结果:Integer。怎么计算调用这说了算。

怎么计算也就是方法行为、行为、行为。

看看执行结果:

我想计算3的平方 = [9]
我考了89分,还差多少分满分 = [11]
我想计算3*2+8-3: = [11]
我想计算1M等于多少k = [1024]

Process finished with exit code 0

如果之前,没有函数式接口,我完成上面的四个任务,要定义四个方法。

然而现在我们只需要定义一个方法,确定好参数类型和返回结果类型,然后在调用方法的时候,传入行为就可以了。

总结:

  • Function接口是函数式接口,代表接受一个参数、输出一个参数的函数。
  • 唯一的函数式方法是apply
  • 范型T 代表传入参数类型,范型R代表返回结果类型。
  • 函数式接口解决了传统方法行为不能作为参数传递给函数的弊端。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值