当前位置: 首页 > news >正文

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

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

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

通过阅读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、LongFunction、DoubleFunction
  • 基本类型的结果:ToIntFunction、ToLongFunction、ToDoubleFunction
  • 基本类型的参数和结果: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->Rcompose(before)、andThen(after)IntFunction、ToIntFunction、IntToLongFunction
Consumervoid accept(T t)T->voidandThen(after)IntConsumer
Predicateboolean test(T t)T->booleanand、negate、orIntPredicate
SupplierT get()*->TIntSupplier、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、ObjLongConsumer、ObjDoubleConsumer
BiPredicate<T, U>
  • 两个参数的谓词(返回布尔值的函数)
  • boolean test(T t, U u)
  • 谓词函数:and、negate、or

使用图表方式总结如下:

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

扩展的运算符

UnaryOperator<T>
  • 一元运算符(单个操作数,生产与操作数类型相同的结果)
  • 继承自Function<T, T>
  • 基本类型的运算符:IntUnaryOperator、LongUnaryOperator、DoubleUnaryOperator
BinaryOperator<T>
  • 二元运算符(两个相同类型的操作数,生产与操作数类型相同的结果)
  • 继承自BiFunction<T, T, T>
  • 基本类型的运算符:IntBinaryOperator、LongBinaryOperator、DoubleBinaryOperator
运算符父类组合方法基本类型专用类
UnaryOperatorFunction<T, T>IntUnaryOperator: int applyAsInt(int operand)
BinaryOperatorBiFunction<T, T, T>minBy、maxByIntBinaryOperator: 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 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)

祝大家玩得开心!ˇˍˇ

简放,杭州

http://www.lryc.cn/news/305300.html

相关文章:

  • Linux TCP 参数设置
  • Dubbo之消费端服务RPC调用
  • 报表控件Stimulsoft 新版本2024.1中,功能区工具栏新功能
  • 零样本带解释性的医学大模型
  • 英文输入法(C 语言)
  • 万众一心 · 喜赢未来,2023宇凡微年会暨阳朔之旅
  • 【Spring】 AOP面向切面编程
  • R语言入门笔记2.6
  • PS人像处理磨皮插件
  • 类型转换(C++)
  • 2.23数据结构
  • c++笔记理解
  • 二进制部署k8s集群之cni网络插件
  • 二维矩阵子集的最大值
  • 瑞_23种设计模式_装饰者模式
  • 使用Python制作进度条有多少种方法?看这一篇文章就够了!
  • SpringBoot-2.7.6基于SLF4J日志门面的日志框架切换
  • MongoDB聚合运算符:$binarySize
  • Android的ViewModel
  • Android 圆环带刻度条进度动画效果实现
  • 94. 二叉树的中序遍历
  • 汽车信息安全概述
  • Linux——基础IO
  • 数据结构-数组
  • 【Java程序设计】【C00279】基于Springboot的智慧外贸平台(有论文)
  • C#,计算几何,计算机图形学(Computer Graphics)洪水填充算法(Flood Fill Algorithm)与源代码
  • C# 实现网页内容保存为图片并生成压缩包
  • C#_事件简述
  • C语言:指针(一)
  • 【leetcode刷题之路】面试经典150题(3)——哈希表+区间