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)
posted @ 2020-06-25 23:20  可苦可乐  阅读(583)  评论(0编辑  收藏  举报