JAVA中Function的使用

这篇具有很好参考价值的文章主要介绍了JAVA中Function的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


参考: https://blog.csdn.net/boyan_HFUT/article/details/99618833

一、方法介绍

表示接受一个参数并产生结果的函数。

参数类型

  • T - 函数输入的类型
  • R - 函数的结果类型

方法介绍

  • R apply(T t)
    将此函数应用于给定的参数。
  • default Function<V, R> compose(Function<? super V, ? extends T> before)
    返回一个组合函数,首先将before函数应用于其输入,然后将此函数应用于结果。 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
  • default Function<T, V> andThen(Function<? super R, ? extends V> after)
    返回一个组合函数,首先将此函数应用于其输入,然后将after函数应用于结果。 如果任一函数的评估引发异常,则将其转发给组合函数的调用者。
  • static Function<T, T> identity()
    返回一个总是返回其输入参数的函数。

源码

@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;
    }
}

二、demo

public class Test {
    public static void main(String[] args) throws Exception {
        Function<Integer, Integer> add = p -> p + 10;
        Integer result = add.apply(10);
        // 这里会输出 20,因为这个函数定义的操作时把参数加上 10 后返回
        System.out.println(result);


        Function<Integer, Integer> multiplyTen = a -> a * 10;
        Function<Integer, Integer> addTen = a -> a + 10;
        // 先增加 10,然后再乘 10,输出结果 110
        Function<Integer, Integer> addTenThenMultiplyTen = multiplyTen.compose(addTen);
        System.out.println(addTenThenMultiplyTen.apply(1));


        // 先乘 10,然后再加 10,输出结果 20
        Function<Integer, Integer> multiplyTenAddTenThen = multiplyTen.andThen(addTen);
        System.out.println(multiplyTenAddTenThen.apply(1));
    }


}

结果
JAVA中Function的使用文章来源地址https://www.toymoban.com/news/detail-410122.html

到了这里,关于JAVA中Function的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • JAVA中Function的使用

    参考: https://blog.csdn.net/boyan_HFUT/article/details/99618833 表示接受一个参数并产生结果的函数。 T - 函数输入的类型 R - 函数的结果类型 R apply(T t) 将此函数应用于给定的参数。 default FunctionV, R compose(Function? super V, ? extends T before) 返回一个组合函数,首先将before函数应用于其输入,

    2023年04月11日
    浏览(43)
  • 解决JDK7调用https报:java.net.SocketException: Connection reset错误

            大多数现代的 HTTPS 连接将使用 TLS 1.2 协议 或 TLS 1.3 协议,具体取决于服务器和客户端支持的版本以及其之间的协商,而 JDK7及以下 版本默认使用是 TLS v1 协议,所以在调用HTTPS接口时,会出现java.net.SocketException: Connection reset报错;         下面是不同JDK版本的默

    2024年04月26日
    浏览(27)
  • Java 中函数 Function 的使用和定义

    模块化和可复用性:将代码逻辑封装在函数中,可以 提高代码的模块化程度,使得代码更易于维护和重用 。通过函数,可以将通用的逻辑抽取出来,降低代码的重复性。 可组合性: 函数可以作为参数传递给其他函数,也可以作为返回值返回 ,从而实现代码的组合和复用。

    2024年04月09日
    浏览(33)
  • Java第一次blog

    7-1 答题判题程序-1 这些题目主要用到 对象与类的处理: 对象是现实世界或抽象概念中的实体在计算机程序中的表示。 类则是具有相同属性和方法的对象的集合,是创建对象的模板。通过类,我们可以定义一类对象的共同特征和行为。 1. 字符串处理: 需要对输入的题目信息

    2024年04月22日
    浏览(39)
  • 为什么要给数据库加索引?转自 https: //blog.tankery.me/development/why-we-need-indexes-for-database

    这篇文章不是数据库索引的使用文档,不会给每个功能的使用都做介绍,而是通过我自己的案例,对案例中遇到的几个点做详细的说明。如果想查看具体的使用帮助,可以参考官网的文档:Query Planning “老谭,测试发现睡眠历史记录页面的打开速度太慢了,你给快速解决一下

    2024年02月03日
    浏览(30)
  • 开箱即用的企业级前后端分离【.NET Core6.0 Api + Vue 2.x + RBAC】权限框架-Blog.Core

    今天要给大家推荐一个开箱即用的企业级前后端分离【.NET Core6.0 Api + Vue 2.x + RBAC】权限框架( 提高生产效率,快速开发就选它 ):Blog.Core。 Blog.Core通过详细的文章和视频讲解,将知识点各个击破,入门ASP.Net Core不再难。 项目功能完善,并且采用流行的前后端分离架构,代码

    2024年01月18日
    浏览(76)
  • 在.NET 6.0上使用Kestrel配置和自定义HTTPS

    引用地址:在.NET 6.0上使用Kestrel配置和自定义HTTPS_kestrel 使用https_云策数据的博客-CSDN博客 在ASP.NET Core中,默认情况下HTTPS处于打开状态,这个不是问题,我们无需禁用它。因为如果你的服务是在防火墙后面,是属于后台服务,不对外网提供服务,启用HTTPS也是有意义的。 通常

    2024年02月16日
    浏览(33)
  • 使用Django Rest framework搭建Blog

    在前面的Blog例子中我们使用的是GraphQL, 虽然GraphQL的使用处于上升趋势,但是Rest API还是使用的更广泛一些. 所以还是决定回到传统的rest api framework上来, Django rest framework的官网上给了一个很好用的QuickStart, 我参考Quick Start将前面的Blog的例子用DRF(Django Rest Framework)重新构筑一遍

    2023年04月19日
    浏览(32)
  • <Articles v-if=“!loading“ :articles=“props.articles“ />的区分

    问: script setup lang=\\\"ts\\\" import { onMounted, ref } from \\\'vue\\\' import { useRoute } from \\\'vue-router\\\' import Articles from \\\'../article/lists.vue\\\' const route = useRoute() const orderby = ref(\\\'hot\\\') const props = defineProps{   articles: ArticleType[]   loading: boolean }() const emit = defineEmits{   (e: \\\'onFilter\\\', json: Recordstring, string): void }()

    2024年01月18日
    浏览(28)
  • CSDN博客批量查询质量分https://yma16.inscode.cc/请求超时问题(设置postman超时时间)(接口提供者设置了nginx超时时间)

    https://yma16.inscode.cc/ 查询别人的一下子就返回了,查询我自己的,1分钟还不返回,然后就显示超时了。。 一开始我还以为是这个开源项目本身的问题,设置了请求超时时间,我还给它改了超时时间,后来发现不是的。。。 本来是100000的,我给改成1000000了,我对js代码不熟,

    2024年02月12日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包