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,杭州