Java 8函数式编程设计(Functional interfaces)
通过阅读JDK 8的java.util.function包源码,意在理解Java的函数式接口设计。
读后自己的理解:Java函数式编程的核心是将最基础的数学函数抽象成接口对象,可在已有的接口上进行积木拼插。最基础的数学函数包括一元函数、谓词、二元函数、运算符计算,对应的Java接口分别是Function、Predicate、BiFunction、BinaryOperator。
API
@FunctionalInterface
Functional interfaces
Function
Consumer
Predicate
Supplier
Use Cases
@FunctionalInterface
函数式接口注解
从概念上讲,函数式接口都只有一个抽象方法。
请注意,可以使用Lambda表达式、方法引用和构造器引用创建函数式接口的实例。
Functional interfaces
函数式接口提供lambda表达式和方法引用的目标类型
每个函数式接口有一个单一的抽象方法,称为函数方法
函数式接口可以匹配或适配为lambda表达式的参数和返回类型
函数式接口可以在多个上下文中提供一个目标类型,如赋值上下文、方法调用上下文、转换上下文
函数式接口往往代表抽象的概念,如函数、操作、谓词
可扩展的命名约定
基本的一元函数:Function、Consumer、Predicate、Supplier
二元函数:BiFunction、BiConsumer、BiPredicate
扩展的运算符:UnaryOperator、BinaryOperator
泛型->基本类型:int、long、double
一元函数
Function
从T到R的一元映射函数,接受一个参数并产生一个结果的一元函数 (类型转换函数)
R apply(T t)
组合函数
compose(before):V -> T -> R
andThen(after):T -> R -> V
基本类型的参数:IntFunction、LongFunction、DoubleFunction
基本类型的结果:ToIntFunction、ToLongFunction、ToDoubleFunction
基本类型的参数和结果:IntToLongFunction、IntToDoubleFunction
Consumer
从T到void的一元函数,接受一个入参但不返回任何结果的操作
void accept(T t)
andThen(after):N个消费者模式,多次消费的场景
基本类型的参数:IntConsumer、LongConsumer、DoubleConsumer
Predicate
一个参数的谓词(返回布尔值的函数)
boolean test(T t)
谓词函数:and、negate、or
基本类型的参数:IntPredicate、LongPredicate、DoublePredicate
Supplier
表示结果的供应商
T get()
基本类型的结果:BooleanSupplier、IntSupplier、LongSupplier、DoubleSupplier
使用图表方式总结如下:
一元函数
方法
类型转换
组合方法
基本类型专用类
Function
R apply(T t)
T->R
compose(before)、andThen(after)
IntFunction、ToIntFunction、IntToLongFunction
Consumer
void accept(T t)
T->void
andThen(after)
IntConsumer
Predicate
boolean test(T t)
T->boolean
and、negate、or
IntPredicate
Supplier
T get()
*->T
IntSupplier、BooleanSupplier
二元函数
BiFunction
从(T、U)到R的二元函数,接受两个参数并产生一个结果的二元函数
R apply(T t, U u)
组合函数
andThen(Function super R, ? extends V> after):(T, U) -> R -> V
基本类型的结果:ToIntBiFunction、ToLongBiFunction、ToDoubleBiFunction
BiConsumer
从(T、U)到void的二元函数,接受两个入参但不返回任何结果的操作
void accept(T t, U u)
基本类型的参数:ObjIntConsumer、ObjLongConsumer、ObjDoubleConsumer
BiPredicate
两个参数的谓词(返回布尔值的函数)
boolean test(T t, U u)
谓词函数:and、negate、or
使用图表方式总结如下:
二元函数
方法
类型转换
组合方法
基本类型专用类
BiFunction
R apply(T t, U u)
(T、U)->R
andThen(Function after)
ToIntBiFunction
BiConsumer
void accept(T t, U u)
(T、U)->void
andThen(after)
ObjIntConsumer
BiPredicate
boolean test(T t, U u)
(T、U)->boolean
and、negate、or
扩展的运算符
UnaryOperator
一元运算符(单个操作数,生产与操作数类型相同的结果)
继承自Function
基本类型的运算符:IntUnaryOperator、LongUnaryOperator、DoubleUnaryOperator
BinaryOperator
二元运算符(两个相同类型的操作数,生产与操作数类型相同的结果)
继承自BiFunction
基本类型的运算符:IntBinaryOperator、LongBinaryOperator、DoubleBinaryOperator
运算符
父类
组合方法
基本类型专用类
UnaryOperator
Function
IntUnaryOperator: int applyAsInt(int operand)
BinaryOperator
BiFunction
minBy、maxBy
IntBinaryOperator: int applyAsInt(int left, int right)
Use Cases
Optional
可能包含null值的容器对象,包装方法的返回结果
empty()、of(T value)、ofNullable(T value)
isPresent()、get()
void ifPresent(Consumer super T> consumer)
Optional filter(Predicate super T> predicate):过滤操作
Optional map(Function super T, ? extends U> mapper)
Optional flatMap(Function super T, Optional> mapper)
T orElse(T other)、T orElseGet(Supplier extends T> other)、T orElseThrow(Supplier extends X> exceptionSupplier) throws X
基本类型实现:OptionalInt、OptionalLong、OptionalDouble
使用模式:
Optional.ifPresent(consumer)
Optional.filter(predicate).flatMap(mapper)
Optional.filter(predicate).map(mapper)
祝玩得开心!ˇˍˇ
云舒,2017.7.26,杭州