JAVA中Function的使用
JAVA中Function的使用
- 一、方法介绍
- 参数类型
- 方法介绍
- 源码
- 二、demo
参考: 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,输出结果 110Function<Integer, Integer> addTenThenMultiplyTen = multiplyTen.compose(addTen);System.out.println(addTenThenMultiplyTen.apply(1));// 先乘 10,然后再加 10,输出结果 20Function<Integer, Integer> multiplyTenAddTenThen = multiplyTen.andThen(addTen);System.out.println(multiplyTenAddTenThen.apply(1));}}
结果
JAVA中Function的使用相关推荐
- java中function实现_Java中的functor实现
文章来源:csdn 作者:wangfengsdu 经常听到回调函数(callback function)这个概念, 所谓回调函数,就是指这个函数先在某处注册,而它将在稍后某个需要的时候被调用.比如在利 ...
- Function接口 – Java8中java.util.function包下的函数式接口
作者: Mohamed Sanaulla 译者: 李璟(jlee381344197@gmail.com) 早先我写了一篇<函数式接口>,探讨了Java8中函数式接口的用法.如果你正在 ...
- java.util接口_Java 8中java.util.function包中的谓词和使用者接口
java.util接口 在上一篇文章中,我写了关于Function接口的内容 ,它是java.util.package的一部分. 我还提到了Predicate接口,它是同一包的一部分,在这篇文章中,我 ...
- java.util接口_函数接口– Java 8中java.util.function包中的函数接口
java.util接口 我以前写过有关功能接口及其用法的文章. 如果您正在探索要成为Java 8一部分的API,尤其是那些支持lambda表达式的API,您会发现很少的接口,例如Function,Su ...
- Java 8中java.util.function包中的谓词和使用者接口
在我以前的文章中,我写了关于Function接口的内容 ,它是java.util.package的一部分. 我还提到了Predicate接口,它是同一包的一部分,在这篇文章中,我将向您展示如何使用Pr ...
- 函数接口– Java 8中java.util.function包中的函数接口
我以前写过有关功能接口及其用法的文章. 如果您正在探索要成为Java 8一部分的API,尤其是那些支持lambda表达式的API,您会发现很少的接口,例如Function,Supplier,Consu ...
- java js中 function函数报错_浅析JS中对函数function的理解(基础篇)
正文:我们知道,在js中,函数实际上是一个对象,每个函数都是Function类型的实例,并且都与其他引用类型一样具有属性和方法.因此,函数名实际上是指向函数对象的指针,不与某个函数绑定.在常见的两种定 ...
- java中的多态_Java中的多态
多态与HoFs 朋友们好久不见啊,最近笔者同时在写脚本型语言--JavaScript,和工业级的面向对象语言--Java. 在写代码的同时呢,也会思考这些语言的不同.今天就拿 Java 中的多态,来谈 ...
- Java中的多态(for myself)
我记得这是大学上课的时候考试最爱考的题目.Java的多态. 多态 Java引用变量时有两个类型: 一个是编译时类型,一个是运行时类型. 编译时类型由申明该变量时使用的类型决定 运行时类型由实际赋值给该 ...
最新文章
- Jmail的主要参数列表
- linux 系统运行状况 shell命令 watch 监控进程是否存在
- CCActionEase想说爱你也不难(上)
- 【重难点】【Java集合 04】ArrayDeque 的使用场景、ArrayBlockingQueue
- 静态成员变量和非静态成员变量
- TransE算法详解
- 【软件测试】软件测试的环境部署和安装教程[全]
- 图文讲解如何在outlook里设置绑定Gmail企业邮箱教程
- couchbase 报 The Content of this Observable is already released. Subscribe earlier or tune the Couch
- kali安装网卡驱动
- 怎么把多个pdf文件合并成一个pdf?
- 经验之谈:做好淘宝客的一些经验秘籍
- 【LeetCode刷题】1619. 删除某些元素后的数组均值
- Xilinx Zynq实现任意波形发生器仿真
- AlexNet -翻译
- 百年包豪斯 (Bauhaus) 对当今 UX 设计的启发与影响
- 区间概念让5千年都无人能识的无穷大自然数一下子暴露出来——数学一直被假自然数集(列)迷惑而将各假N误为N
- 基于cp-abe算法的访问控制方法在linux下的实现和算法优化,基于CP-ABE的访问控制研究...
- 手机CAD3D 建模APP:CAD 建模号(WuWeido)使用效果
- zookeeper设置retries