目录

Supplier

参数个数扩展

参数类型扩展

特殊变形

Function,r>

参数个数扩展

参数类型扩展

特殊变形

Consumer

参数个数扩展

参数类型扩展

特殊变形

Predicate

参数个数扩展

参数类型扩展

特殊变形


java.util.function包下主要为函数接口,主要包含4类函数接口:

  • Supplier<T>: 数据提供器,可以提供 T 类型对象;无参的构造器,提供了 get 方法;
  • Function<T,R>: 数据转换器,接收一个 T 类型的对象,返回一个 R类型的对象; 单参数单返回值的行为接口;提供了 apply, compose, andThen, identity 方法;
  • Consumer<T>: 数据消费器, 接收一个 T类型的对象,无返回值,通常用于设置T对象的值; 单参数无返回值的行为接口;提供了 accept, andThen 方法;
  • Predicate<T>: 条件测试器,接收一个 T 类型的对象,返回布尔值,通常用于传递条件函数; 单参数布尔值的条件性接口。提供了 test (条件测试) , and-or- negate(与或非) 方法。

Consumer与Function都有andThen方法,都返回一个function,但是意义不一样。

Consumer是在输入参数上先应用this对象的apply,再应用after的apply。

Function是先应用this对象的apply()方法,产生一个结果,再把此结果作为输入参数,应用到after的apply()方法。

Consumer:

(T t, U u) -> after.apply(apply(t, u));

Function:

(T t) -> after.apply(apply(t));

Supplier<T>

@FunctionalInterface
public interface Supplier<T> {

/**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

参数个数扩展

参数类型扩展

BooleanSupplier

public interface BooleanSupplier {
    boolean getAsBoolean();
}

DoubleSupplier

public interface DoubleSupplier {
    double getAsDouble();
}

IntSupplier

public interface IntSupplier {
    int getAsInt();
}

LongSupplier

public interface LongSupplier {
    long getAsLong();
}

特殊变形

Function<T,R>

@FunctionalInterface
public interface Function<T, R> {

/**
     * Applies this function to the given argument.
     */
    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.
     */
    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);

//before.apply(v):输入类型V,返回类型T

//最终函数:输入类型T,返回类型R
        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.
     */
    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);

//apply(t):输入类型T,返回类型R

//最终函数:输入类型R,返回类型V

return (T t) -> after.apply(apply(t));
    }

/**
     * Returns a function that always returns its input argument.
     */
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

参数个数扩展

BiFunction

public interface BiFunction<T, U, R> {
    R apply(T t, U u);

default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t, U u) -> after.apply(apply(t, u));
    }
}

ToDoubleBiFunction

public interface ToDoubleBiFunction<T, U> {

double applyAsDouble(T t, U u);
}

ToIntBiFunction

public interface ToIntBiFunction<T, U> {

int applyAsInt(T t, U u);
}

ToLongBiFunction

public interface ToLongBiFunction<T, U> {
    long applyAsLong(T t, U u);
}

参数类型扩展

DoubleFunction

public interface DoubleFunction<R> {

R apply(double value);
}

DoubleToIntFunction

public interface DoubleToIntFunction {

int applyAsInt(double value);
}

DoubleToLongFunction

public interface DoubleToLongFunction {

long applyAsLong(double value);
}

IntFunction

public interface IntFunction<R> {

R apply(int value);
}

IntToDoubleFunction

public interface IntToDoubleFunction {

double applyAsDouble(int value);
}

IntToLongFunction

public interface IntToLongFunction {

long applyAsLong(int value);
}

LongFunction

public interface LongFunction<R> {

R apply(long value);
}

LongToDoubleFunction

public interface LongToDoubleFunction {

double applyAsDouble(long value);
}

LongToIntFunction

public interface LongToIntFunction {

int applyAsInt(long value);
}

ToDoubleFunction

public interface ToDoubleFunction<T> {

double applyAsDouble(T value);
}

ToIntFunction

public interface ToIntFunction<T> {

int applyAsInt(T value);
}

ToLongFunction

public interface ToLongFunction<T> {

long applyAsLong(T value);
}

特殊变形

UnaryOperator

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

//返回值是一个Function
    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

IntUnaryOperator

public interface IntUnaryOperator {

int applyAsInt(int operand);

//返回值为一个Function
    default IntUnaryOperator compose(IntUnaryOperator before) {
        Objects.requireNonNull(before);
        return (int v) -> applyAsInt(before.applyAsInt(v));
    }

//返回值为一个Function
    default IntUnaryOperator andThen(IntUnaryOperator after) {
        Objects.requireNonNull(after);
        return (int t) -> after.applyAsInt(applyAsInt(t));
    }

static IntUnaryOperator identity() {
        return t -> t;
    }
}

LongUnaryOperator

public interface LongUnaryOperator {

long applyAsLong(long operand);

default LongUnaryOperator compose(LongUnaryOperator before) {
        Objects.requireNonNull(before);
        return (long v) -> applyAsLong(before.applyAsLong(v));
    }

default LongUnaryOperator andThen(LongUnaryOperator after) {
        Objects.requireNonNull(after);
        return (long t) -> after.applyAsLong(applyAsLong(t));
    }

static LongUnaryOperator identity() {
        return t -> t;
    }
}

DoubleUnaryOperator

public interface DoubleUnaryOperator {

double applyAsDouble(double operand);

default DoubleUnaryOperator compose(DoubleUnaryOperator before) {
        Objects.requireNonNull(before);
        return (double v) -> applyAsDouble(before.applyAsDouble(v));
    }

default DoubleUnaryOperator andThen(DoubleUnaryOperator after) {
        Objects.requireNonNull(after);
        return (double t) -> after.applyAsDouble(applyAsDouble(t));
    }

static DoubleUnaryOperator identity() {
        return t -> t;
    }
}

BinaryOperator

public interface BinaryOperator<T> extends BiFunction<T,T,T> {

public static <T> BinaryOperator<T> minBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) <= 0 ? a : b;
    }

public static <T> BinaryOperator<T> maxBy(Comparator<? super T> comparator) {
        Objects.requireNonNull(comparator);
        return (a, b) -> comparator.compare(a, b) >= 0 ? a : b;
    }
}

DoubleBinaryOperator

public interface DoubleBinaryOperator {

double applyAsDouble(double left, double right);
}

IntBinaryOperator

public interface IntBinaryOperator {

int applyAsInt(int left, int right);
}

LongBinaryOperator

public interface LongBinaryOperator {

long applyAsLong(long left, long right);
}

Consumer<T>

@FunctionalInterface
public interface Consumer<T> {

/**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

/**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);

//accept(t):输入类型T,无返回值。

//继续应用after.accept,输入类型T
        return (T t) -> { accept(t); after.accept(t); };
    }
}

参数个数扩展

参数类型扩展

DoubleConsumer

public interface DoubleConsumer {

void accept(double value);

default DoubleConsumer andThen(DoubleConsumer after) {
        Objects.requireNonNull(after);
        return (double t) -> { accept(t); after.accept(t); };
    }
}

IntConsumer

public interface IntConsumer {

void accept(int value);

default IntConsumer andThen(IntConsumer after) {
        Objects.requireNonNull(after);
        return (int t) -> { accept(t); after.accept(t); };
    }
}

LongConsumer

public interface LongConsumer {

void accept(long value);

default LongConsumer andThen(LongConsumer after) {
        Objects.requireNonNull(after);
        return (long t) -> { accept(t); after.accept(t); };
    }
}

ObjDoubleConsumer

public interface ObjDoubleConsumer<T> {

void accept(T t, double value);
}

ObjIntConsumer

public interface ObjIntConsumer<T> {

void accept(T t, int value);
}

ObjLongConsumer

public interface ObjLongConsumer<T> {

void accept(T t, long value);
}

特殊变形

Predicate<T>

@FunctionalInterface
public interface Predicate<T> {

/**
     * Evaluates this predicate on the given argument.
     */
    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.
     */
    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.
     */
    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.
 
     */
    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)}.
     */
    static <T> Predicate<T> isEqual(Object targetRef) {
        return (null == targetRef)
                ? Objects::isNull
                : object -> targetRef.equals(object);
    }
}

参数个数扩展

BiPredicate

public interface BiPredicate<T, U> {

boolean test(T t, U u);

default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other) {
        Objects.requireNonNull(other);
        return (T t, U u) -> test(t, u) && other.test(t, u);
    }

default BiPredicate<T, U> negate() {
        return (T t, U u) -> !test(t, u);
    }

default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other) {
        Objects.requireNonNull(other);
        return (T t, U u) -> test(t, u) || other.test(t, u);
    }
}

参数类型扩展

DoublePredicate

public interface DoublePredicate {

boolean test(double value);

default DoublePredicate and(DoublePredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) && other.test(value);
    }

default DoublePredicate negate() {
        return (value) -> !test(value);
    }

default DoublePredicate or(DoublePredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) || other.test(value);
    }
}

IntPredicate

public interface IntPredicate {

boolean test(int value);

default IntPredicate and(IntPredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) && other.test(value);
    }

default IntPredicate negate() {
        return (value) -> !test(value);
    }

default IntPredicate or(IntPredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) || other.test(value);
    }
}

LongPredicate

public interface LongPredicate {

boolean test(long value);

default LongPredicate and(LongPredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) && other.test(value);
    }

default LongPredicate negate() {
        return (value) -> !test(value);
    }

default LongPredicate or(LongPredicate other) {
        Objects.requireNonNull(other);
        return (value) -> test(value) || other.test(value);
    }
}

特殊变形

java.util.function包相关推荐

  1. 深入学习Java8 Lambda (default method, lambda, function reference, java.util.function 包)

    Java 8 Lambda .MethodReference.function包 多年前,学校讲述C#时,就已经知道有Lambda,也惊喜于它的方便,将函数式编程方式和面向对象式编程基于一身.此外在使 ...

  2. java.util接口_Java 8中java.util.function包中的谓词和使用者接口

    java.util接口 在上一篇文章中,我写了关于Function接口的内容 ,它是java.util.package的一部分. 我还提到了Predicate接口,它是同一包的一部分,在这篇文章中,我 ...

  3. java.util接口_函数接口– Java 8中java.util.function包中的函数接口

    java.util接口 我以前写过有关功能接口及其用法的文章. 如果您正在探索要成为Java 8一部分的API,尤其是那些支持lambda表达式的API,您会发现很少的接口,例如Function,Su ...

  4. Java 8中java.util.function包中的谓词和使用者接口

    在我以前的文章中,我写了关于Function接口的内容 ,它是java.util.package的一部分. 我还提到了Predicate接口,它是同一包的一部分,在这篇文章中,我将向您展示如何使用Pr ...

  5. 函数接口– Java 8中java.util.function包中的函数接口

    我以前写过有关功能接口及其用法的文章. 如果您正在探索要成为Java 8一部分的API,尤其是那些支持lambda表达式的API,您会发现很少的接口,例如Function,Supplier,Consu ...

  6. Function接口 – Java8中java.util.function包下的函数式接口

    作者:   Mohamed Sanaulla  译者: 李璟(jlee381344197@gmail.com) 早先我写了一篇<函数式接口>,探讨了Java8中函数式接口的用法.如果你正在 ...

  7. Java使用Function包策略模式,优化业务代码大量if...else语句

    目录 场景模拟 Consumer与ToIntBiFunction简介,u> 场景Demo业务代码改造 最终结果 业务代码中,若存在大量无法避免的if...else代码,可以尝试使用JDK8提供的 ...

  8. Java高并发编程学习(三)java.util.concurrent包

    简介 我们已经学习了形成Java并发程序设计基础的底层构建块,但对于实际编程来说,应该尽可能远离底层结构.使用由并发处理的专业人士实现的较高层次的结构要方便得多.要安全得多.例如,对于许多线程问题,可 ...

  9. java.util.concurrent包API学习笔记

    newFixedThreadPool 创建一个固定大小的线程池. shutdown():用于关闭启动线程,如果不调用该语句,jvm不会关闭. awaitTermination():用于等待子线程结束, ...

最新文章

  1. MPB:北大口腔陈峰、陈智滨等-​口腔微生物组研究主要取样部位及方法
  2. mantelhean.test r语言_R语言基础-检验与分析函数
  3. 【人工智能】人类该如何看待人工智能的“诗与远方”?
  4. 防护很重要!教你教你认识和检验安防产品的IP防护等级
  5. 连接远程数据库ORACLE11g,错误百出!
  6. 集成开发环境(IDE)
  7. 5.fork和vfork
  8. springmvc+jsp引用本地图片文件
  9. 字符识别(模板匹配BP神经网络训练)
  10. 4982亿背后的前端技术—2020天猫双11前端体系大揭秘
  11. (王道408考研操作系统)第三章内存管理-第二节4:页面分配策略
  12. HDU-1994-利息计算
  13. 拼多多Java面试题、笔试题(含答案)
  14. speex回声消除源码解读
  15. sizeof运算符与求字符串长度函数strlen的区别
  16. python绘制柱状图和折线图_python绘制散点图,柱状图和折线图
  17. 导航路径规划之四 路径规划概述
  18. vue导入excel进度条_纯前端 vue+ js-xlsx 导入excel表格
  19. matlab 雅各比符号,密码学与编码理论(第2版)(密码学方面的经典著作)
  20. Glusterfs全局统一命名空间

热门文章

  1. 网址收藏 plc实现
  2. 【大话设计模式】设计模式系统学习大合集
  3. (一)html5中的新增元素和废除元素
  4. 【特征选择】基础知识
  5. 对于人工智能的学习有哪些建议?【转】
  6. shell脚本练习(12.8)
  7. 如果测试你的MongoDB应用升级?
  8. 从零开始学习Sencha Touch MVC应用之十九
  9. 医疗卫生信息化 医学信息 医院管理 医疗信息化 资源下载
  10. ASP.NET页面的处理过程完全版_AX