142-java-functional-programming

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Java Functional Programming rules

Java 函数式编程规则

Identify and apply functional programming principles in Java to improve immutability, expressiveness, and maintainability.
What is covered in this Skill?
  • Immutable objects and Records (JEP 395)
  • Pure functions free of side effects
  • Functional interfaces:
    Function
    ,
    Predicate
    ,
    Consumer
    ,
    Supplier
    , custom
    @FunctionalInterface
  • Lambda expressions and method references
  • Stream API: filter/map/reduce pipelines, parallel streams,
    toUnmodifiable*
    collectors
  • Optional
    idiomatic usage:
    map
    /
    flatMap
    /
    filter
    /
    orElse*
    over
    isPresent()
    +
    get()
  • Function composition:
    andThen
    /
    compose
  • Higher-order functions: memoization, currying, partial application
  • Pattern Matching for
    instanceof
    and
    switch
    (Java 21)
  • Sealed classes and interfaces (Java 17) for exhaustive domain hierarchies
  • Switch Expressions (Java 14), Stream Gatherers (JEP 461)
  • Effect-boundary separation: side effects at edges, pure core logic
  • Immutable collections:
    List.of()
    ,
    Collectors.toUnmodifiableList()
Scope: The reference is organized by examples (good/bad code patterns) for each core area. Apply recommendations based on applicable examples.
识别并在Java中应用函数式编程原则,以提升不可变性、表达性和可维护性。
本Skill涵盖哪些内容?
  • 不可变对象与Records(JEP 395)
  • 无副作用的纯函数
  • 函数式接口:
    Function
    Predicate
    Consumer
    Supplier
    、自定义
    @FunctionalInterface
  • Lambda表达式与方法引用
  • Stream API:filter/map/reduce管道、并行流、
    toUnmodifiable*
    收集器
  • Optional
    的惯用用法:优先使用
    map
    /
    flatMap
    /
    filter
    /
    orElse*
    而非
    isPresent()
    +
    get()
  • 函数组合:
    andThen
    /
    compose
  • 高阶函数:记忆化、柯里化、部分应用
  • instanceof
    和switch的模式匹配(Java 21)
  • 密封类与接口(Java 17):用于穷尽式领域层级
  • Switch表达式(Java 14)、Stream Gatherers(JEP 461)
  • 副作用边界分离:副作用仅在边缘处理,核心逻辑保持纯函数
  • 不可变集合:
    List.of()
    Collectors.toUnmodifiableList()
范围:参考文档按每个核心领域的示例(好/坏代码模式)组织。请根据适用的示例应用建议。

Constraints

约束条件

Before applying any functional programming changes, ensure the project compiles. If compilation fails, stop immediately — do not proceed until the project compiles successfully. Verify that maven-compiler-plugin source/target supports the Java features being used.
  • MANDATORY: Run
    ./mvnw compile
    or
    mvn compile
    before applying any changes
  • SAFETY: If compilation fails, stop immediately — do not proceed until the project compiles successfully
  • VERIFY: Verify maven-compiler-plugin source/target supports the Java features being used
  • VERIFY: Run
    ./mvnw clean verify
    or
    mvn clean verify
    after applying improvements
  • BEFORE APPLYING: Read the reference for detailed good/bad examples, constraints, and safeguards for each functional programming pattern
在应用任何函数式编程变更之前,确保项目能够编译。如果编译失败,请立即停止——在项目成功编译前不要继续。验证maven-compiler-plugin的source/target是否支持所使用的Java特性。
  • 强制要求:在应用任何变更前运行
    ./mvnw compile
    mvn compile
  • 安全提示:如果编译失败,请立即停止——在项目成功编译前不要继续
  • 验证:确认maven-compiler-plugin的source/target支持所使用的Java特性
  • 验证:在应用改进后运行
    ./mvnw clean verify
    mvn clean verify
  • 应用前:阅读参考文档,了解每个函数式编程模式的详细好/坏示例、约束条件和防护措施

When to use this skill

何时使用本Skill

  • Improve the code with Functional Programming
  • Apply Functional Programming
  • Refactor the code with Functional Programming
  • 使用函数式编程改进代码
  • 应用函数式编程
  • 使用函数式编程重构代码

Reference

参考资料

For detailed guidance, examples, and constraints, see references/142-java-functional-programming.md.
如需详细指导、示例和约束条件,请查看references/142-java-functional-programming.md