首页 > 编程语言 >Java8提供的函数接口

Java8提供的函数接口

时间:2022-09-24 11:59:46浏览次数:76  
标签:function composed predicate code return 函数 接口 other Java8

目录

参考资料

函数接口

Java 提供的函数接口位于 java.util.function 包下。

接下来介绍主要的函数接口:

1. Function<T, R>

Function 接口代表一个 接收1个参数并返回1个结果的函数。

Function 接口源码:

@FunctionalInterface
public interface Function<T, R> {

    /**
     * 将参数 t 应用到函数,并返回结果
     *
     * @param t 函数参数
     * @return 函数结果
     */
    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));
    }

    /**
     * 静态方法,生成一个结果等于输入参数的函数
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

2. Predicate

Predicate 接口代表对一个参数的断言。
Predicate 接口代码:

@FunctionalInterface
public interface Predicate<T> {

    /**
     * 对入参 t 进行断言(断言,相当于判断)
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * AND of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code false}, then the {@code other}
     * predicate is not evaluated.
     *
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.
     *
     * @param other a predicate that will be logically-ANDed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * AND of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> and(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) && other.test(t);
    }

    /**
     * Returns a predicate that represents the logical negation of this
     * predicate.
     *
     * @return a predicate that represents the logical negation of this
     * predicate
     */
    default Predicate<T> negate() {
        return (t) -> !test(t);
    }

    /**
     * Returns a composed predicate that represents a short-circuiting logical
     * OR of this predicate and another.  When evaluating the composed
     * predicate, if this predicate is {@code true}, then the {@code other}
     * predicate is not evaluated.
     *
     * <p>Any exceptions thrown during evaluation of either predicate are relayed
     * to the caller; if evaluation of this predicate throws an exception, the
     * {@code other} predicate will not be evaluated.
     *
     * @param other a predicate that will be logically-ORed with this
     *              predicate
     * @return a composed predicate that represents the short-circuiting logical
     * OR of this predicate and the {@code other} predicate
     * @throws NullPointerException if other is null
     */
    default Predicate<T> or(Predicate<? super T> other) {
        Objects.requireNonNull(other);
        return (t) -> test(t) || other.test(t);
    }

    /**
     * Returns a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}.
     *
     * @param <T> the type of arguments to the predicate
     * @param targetRef the object reference with which to compare for equality,
     *               which may be {@code null}
     * @return a predicate that tests if two arguments are equal according
     * to {@link Objects#equals(Object, Object)}
     */
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}


3.Supplier

Supplier 接口代表结果的供应商。
Supplier 接口代码:

@FunctionalInterface
public interface Supplier<T> {

    /**
     * 获取一个结果
     *
     * @return a result
     */
    T get();
}

4.UnaryOperator

UrnaryOperator 接口代表一元操作符,即返回结果类型和输入参数类型一样。
UnaryOperator 接口代码:


@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {

    /**
     * 返回结果等于入参的一元操作符
     *
     * @param <T> the type of the input and output of the operator
     * @return a unary operator that always returns its input argument
     */
    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

标签:function,composed,predicate,code,return,函数,接口,other,Java8
From: https://www.cnblogs.com/lihw-study/p/16725276.html

相关文章

  • 函数递归
    CREATEDEFINER=`root`@`%`FUNCTION`queryParentAreaInfo`(areaIdINT)RETURNSvarchar(4000)CHARSETutf8mb4BEGINDECLAREsTempVARCHAR(4000);DECLAREsTempChd......
  • python中round函数的一个小坑——奇进偶弃
    python中round函数的一个小坑——奇进偶弃 >>>round(3.1415,3)3.142>>>round(4.1415,3)4.141 >>>round(10.5)10>>>round(11.5)12 ......
  • 函数
    函数:一个功能体,提供若干的数据,返回处理的结果,函数分为系统函数和自定义函数==>函数是为了封装重复执行的代码系统函数:Number()/parseInt()..​1.创建自定义函数functio......
  • 函数加强--学员管理系统1
    一、应用:学院管理系统1.1系统简介需求:进入系统显示系统功能界面1)添加学员2)删除学员3)修改学员信息4......
  • API接口
    一、前后端开发模式#以前开发项目是前端写好静态文件然后后端再用模板语法套到这个静态文件中之后衍生出了全栈开发就是前端后端都一个人写#然就现在逐渐开始前......
  • API接口与drf规范
    前后端开发模式1.前后端混合前端写好静态html页面,后端使用模板语法进行渲染,然后在在对接,遇到问题返回给前端进行修改,这要求后端人员会一些HTML、JS等前端语言,这种模式让......
  • 接口基础
    1、什么是API接口?简单来说,接口就是系统或模块之间相互连接的部分,就称为接口。在软件测试中,我们可以理解为url就是一个接口,如:https://www.baidu.com/由客户端(浏览器)发......
  • 前后端开发模式、API接口、接口测试工具postman、restful规范、序列化和反序列化、dja
    目录前后端开发模式一、两种模式1.传统开发模式:前后端混合开发1.1.缺点:2.前后端分离开发模式2.1.特点3.补充老刘的相关博客:二、API接口1.作用2.说明三、接口测试工具postm......
  • 今日内容 API接口和drf的使用
    前后端开发模式详细见博客链接:https://www.cnblogs.com/liuqingzheng/p/10900502.html补充:前后端混合开发使用模板语法渲染模板后端人员要通过前端写好的html页面......
  • 集合.Set子接口
    Set子接口特点:无序、无下标、元素不可重复方法:全部继承自Collection中的方法Set实现类HashSet【重点】:基于HashCode实现元素不重复当存入元素的哈希码相同时,会调......