• JDK 8中重要的函数式接口(必知必会)


    JDK 8 提供的重要函数式接口:

    Consumer (消费者)

    • 功能:接收一个对象,返回void。
    • 定义:void accept(T t)
    • 默认方法:Consumer andThen(Consumer after)

    BiConsumer (双参消费者)

    • 功能:接收两个对象,返回void。
    • 定义:void accept(T t, U u)
    • 默认方法:BiConsumer andThen(BiConsumer after)

    Supplier (提供者)

    • 功能:不接收参数,返回一个对象。
    • 定义:T get()
    • 默认方法:无

    Function<T, R> (单参函数)

    • 功能:接收一个参数,返回一个参数。可理解为初阶函数 f(x)
    • 定义:R apply(T t)
    • 默认方法:
    1. 组合 Function<V, R> compose(Function before)
    2. 颠倒组合 Function<V, R> compose(Function after)
    3. 返回自身 Function<T, T> identity()

    UnaryOperator<T> extends Function<T, T> (同类型单参函数)

    • 功能:接收一个参数,返回一个同类型参数。
    • 定义:T apply(T t)
    • 默认方法:
    1. 返回自身 UnaryOperator<T> identity()

    BiFunction<T, U, R> (双参函数)

    • 功能:接收两个参数,返回一个参数。可理解为初阶函数 f(x,y)
    • 定义:R apply(T t, U u)
    • 默认方法:BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after)

    BinaryOperator<T> extends BiFunction<T,T,T> (同类型双参操作)

    • 功能:接收两个同类型参数,返回一个同类型参数。
    • 定义:T apply(T t1, T t2)
    • 默认方法:
    1. BinaryOperator<T> minBy(Comparator<? super T> comparator)
    2. BinaryOperator<T> maxBy(Comparator<? super T> comparator)

    Predicate<T> (断言)

    • 功能:接收一个参数,返回一个布尔值。
    • 定义:boolean test(T t)
    • 默认方法:
    1. Predicate<T> and(Predicate<? super T> other)
    2. Predicate<T> or(Predicate<? super T> other)
    3. Predicate<T> negate()
    4. Predicate<T> isEqual(Object targetRef)
  • 相关阅读:
    桟错误分析方法
    gstreamer调试命令
    sqlite的事务和锁,很透彻的讲解 【转】
    严重: Exception starting filter struts2 java.lang.NullPointerException (转载)
    eclipse 快捷键
    POJ 1099 Square Ice
    HDU 1013 Digital Roots
    HDU 1087 Super Jumping! Jumping! Jumping!(动态规划)
    HDU 1159 Common Subsequence
    HDU 1069 Monkey and Banana(动态规划)
  • 原文地址:https://www.cnblogs.com/1626ace/p/13193424.html
Copyright © 2020-2023  润新知