142-java-functional-programming
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseJava 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, customSupplier@FunctionalInterface - Lambda expressions and method references
- Stream API: filter/map/reduce pipelines, parallel streams, collectors
toUnmodifiable* - idiomatic usage:
Optional/map/flatMap/filteroverorElse*+isPresent()get() - Function composition: /
andThencompose - Higher-order functions: memoization, currying, partial application
- Pattern Matching for and
instanceof(Java 21)switch - 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() - 函数组合:/
andThencompose - 高阶函数:记忆化、柯里化、部分应用
- 和switch的模式匹配(Java 21)
instanceof - 密封类与接口(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 or
./mvnw compilebefore applying any changesmvn compile - 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 or
./mvnw clean verifyafter applying improvementsmvn clean verify - 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 compilemvn compile - 安全提示:如果编译失败,请立即停止——在项目成功编译前不要继续
- 验证:确认maven-compiler-plugin的source/target支持所使用的Java特性
- 验证:在应用改进后运行或
./mvnw clean verifymvn 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。