Java-8函数式编程设计-Functional-Interface

Java 8函数式编程设计(Functional interfaces)

我自己的理解,函数式编程对用户最大的价值是促使开发者养成模块化编程的习惯,代码可读性和维护性提高很多。

通过阅读JDK 8的 java.util.function
java.util.stream 包源码,意在理解Java的函数式接口设计。

读后自己的理解:Java函数式编程的核心是将最基础的数学函数抽象成接口对象,可在已有的接口上进行积木拼插组合,形成完整地类型转换系统。最基础的数学函数包括一元函数、谓词、二元函数、运算符计算,对应的Java接口分别是Function、Predicate、BiFunction、BinaryOperator。

源码阅读笔记:jdk-8-Functional-Interface


函数式接口API

  • @FunctionalInterface
  • Functional interfaces
    • Function
    • Consumer
    • Predicate
    • Supplier
  • 数据流特性和操作
    • BaseStream
    • Stream
  • 使用示例

@FunctionalInterface

  • 函数式接口注解
  • 从概念上讲,函数式接口都只有一个抽象方法
  • 请注意,可以使用Lambda表达式、方法引用和构造器引用创建函数式接口的实例。

Functional interfaces

  • 函数式接口提供lambda表达式和方法引用的目标类型
  • 每个函数式接口有一个单一的抽象方法,称为函数方法
  • 函数式接口可以匹配或适配为lambda表达式的参数和返回类型
  • 函数式接口可以在多个上下文中提供一个目标类型,如赋值上下文、方法调用上下文、转换上下文
  • 函数式接口往往代表抽象的概念,如函数、操作、谓词
  • 可扩展的命名约定
  • 基本的一元函数:Function、Consumer、Predicate、Supplier
  • 二元函数:BiFunction、BiConsumer、BiPredicate
  • 扩展的运算符:UnaryOperator、BinaryOperator
  • 泛型->基本类型:int、long、double

一元函数

Function<T, R>

  • 从T到R的一元映射函数,接受一个参数并产生一个结果的一元函数 (类型转换函数)
  • R apply(T t)
  • 组合函数
  • compose(before):V -> T -> R
  • andThen(after):T -> R -> V
  • 基本类型的参数:IntFunction<R>、LongFunction<R>、DoubleFunction<R>
  • 基本类型的结果:ToIntFunction<T>、ToLongFunction<T>、ToDoubleFunction<T>
  • 基本类型的参数和结果:IntToLongFunction、IntToDoubleFunction

Consumer<T>

  • 从T到void的一元函数,接受一个入参但不返回任何结果的操作
  • void accept(T t)
  • andThen(after):N个消费者模式,多次消费的场景
  • 基本类型的参数:IntConsumer、LongConsumer、DoubleConsumer

Predicate<T>

  • 一个参数的谓词(返回布尔值的函数)
  • boolean test(T t)
  • 谓词函数:and、negate、or
  • 基本类型的参数:IntPredicate、LongPredicate、DoublePredicate

Supplier<T>

  • 表示结果的供应商
  • T get()
  • 基本类型的结果:BooleanSupplier、IntSupplier、LongSupplier、DoubleSupplier

使用图表方式总结如下:

一元函数 方法 类型转换 组合方法 基本类型专用类
Function<T, R> R apply(T t) T->R compose(before)、andThen(after) IntFunction<R>、ToIntFunction<T>、IntToLongFunction
Consumer<T> void accept(T t) T->void andThen(after) IntConsumer
Predicate<T> boolean test(T t) T->boolean and、negate、or IntPredicate
Supplier<T> T get() *->T IntSupplier、BooleanSupplier

二元函数

BiFunction<T, U, R>

  • 从(T、U)到R的二元函数,接受两个参数并产生一个结果的二元函数
  • R apply(T t, U u)
  • 组合函数
  • andThen(Function<? super R, ? extends V> after):(T, U) -> R -> V
  • 基本类型的结果:ToIntBiFunction<T, U>、ToLongBiFunction<T, U>、ToDoubleBiFunction<T, U>

BiConsumer<T, U>

  • 从(T、U)到void的二元函数,接受两个入参但不返回任何结果的操作
  • void accept(T t, U u)
  • 基本类型的参数:ObjIntConsumer<T>、ObjLongConsumer<T>、ObjDoubleConsumer<T>

BiPredicate<T, U>

  • 两个参数的谓词(返回布尔值的函数)
  • boolean test(T t, U u)
  • 谓词函数:and、negate、or

使用图表方式总结如下:

二元函数 方法 类型转换 组合方法 基本类型专用类
BiFunction<T, U, R> R apply(T t, U u) (T、U)->R andThen(Function after) ToIntBiFunction<T, U>
BiConsumer<T, U> void accept(T t, U u) (T、U)->void andThen(after) ObjIntConsumer<T>
BiPredicate<T, U> boolean test(T t, U u) (T、U)->boolean and、negate、or

扩展的运算符

UnaryOperator<T>

  • 一元运算符(单个操作数,生产与操作数类型相同的结果)
  • 继承自Function<T, T>
  • 基本类型的运算符:IntUnaryOperator、LongUnaryOperator、DoubleUnaryOperator

BinaryOperator<T>

  • 二元运算符(两个相同类型的操作数,生产与操作数类型相同的结果)
  • 继承自BiFunction<T, T, T>
  • 基本类型的运算符:IntBinaryOperator、LongBinaryOperator、DoubleBinaryOperator
运算符 父类 组合方法 基本类型专用类
UnaryOperator<T> Function<T, T> IntUnaryOperator: int applyAsInt(int operand)
BinaryOperator<T> BiFunction<T, T, T> minBy、maxBy IntBinaryOperator: int applyAsInt(int left, int right)

数据流操作

BaseStream<T,S extends BaseStream<T,S>>

数据流操作特性

  • 顺序流
  • 并行流
  • 拆分器
  • 关闭数据流(数据流管道)

Stream<T>

数据流

  • 中间操作,返回Stream<T>
    • filter
    • distinct
    • map
    • flatMap
    • peek
  • 终结操作,返回结果/Optional<T>
    • collect
    • forEach
    • reduce
    • anyMatch
    • findAny
    • findFirst
    • count
    • max
    • min

使用示例

Optional<T>

单值数据流

  • 可能包含null值的容器对象,包装方法的返回结果
  • empty()、of(T value)、ofNullable(T value)
  • isPresent()、get()
  • void ifPresent(Consumer<? super T> consumer)
  • Optional<T> filter(Predicate<? super T> predicate):过滤操作
  • Optional<U> map(Function<? super T, ? extends U> mapper)
  • Optional<U> flatMap(Function<? super T, Optional<U>> mapper)
  • T orElse(T other)、T orElseGet(Supplier<? extends T> other)、T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X
  • 基本类型实现:OptionalInt、OptionalLong、OptionalDouble
  • 使用模式:
    • Optional<T>.ifPresent(consumer)
    • Optional<T>.filter(predicate).flatMap(mapper)
    • Optional<T>.filter(predicate).map(mapper)

祝玩得开心!ˇˍˇ
云舒,2017.7.26,杭州

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容