Java8新特性Function、BiFunction使用
闲话不多说,直接看代码,注释都写的很清楚了。
package com; import java.util.function.BiFunction; import java.util.function.Function; public class DemoFunction { public static void main(String[] args) { DemoFunction t1 = new DemoFunction(); // Function函数的使用 Integer addResult = t1.compute(3, value -> value + value); System.out.println("加法结果:" + addResult); Integer subResult = t1.compute(3, value -> value - 1); System.out.println("减法结果:" + subResult); Integer multipResult = t1.compute(3, value -> value * value); System.out.println("乘法结果:" + multipResult); Integer divisionResult = t1.compute(6, value -> value / 3); System.out.println("除法结果:" + divisionResult); // 使用compose场景, 从右向左处理, 这里就是 (6 * 6) + 10 = 46 Integer composeResult = t1.computeForCompose(6, value -> value + 10, value -> value * value); System.out.println("Function compose 结果:" + composeResult); // 使用andThen场景, 从左向右处理, 这里就是(3 + 20) - 10 = 13 Integer andThenResult = t1.computeForAndThen(3, value -> value + 20, value -> value - 10); System.out.println("Function andThen 结果:" + andThenResult); // 使用 BiFunctioin场景, 这里是 2 + 3 = 5 Integer biFuncResult = t1.computeForBiFunction(2, 3, (v1, v2) -> v1 + v2); System.out.println("BiFunction 结果:" + biFuncResult); // 使用 BiFunctioin andThen场景, 这里是 (2 * 3) + 6 = 12 Integer biFuncAndThenResult = t1.computeForBiFunctionAndThen(2, 3, (v1, v2) -> v1 * v2, v1 -> v1 + 6); System.out.println("BiFunction andThen 结果:" + biFuncAndThenResult); } /** * @param num * @param function * @return * @desc 使用JDK8 Function函数 */ private Integer compute(Integer num, Function<Integer, Integer> function) { Integer result = function.apply(num); return result; } /** * @param num * @param function1 * @param function2 * @return * @desc 使用compose函数,简单的说,就是从右向左处理。 */ private Integer computeForCompose(Integer num, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.compose(function2).apply(num); } /** * @param num * @param function1 * @param function2 * @return * @desc 使用andThen函数,简单的说,就是从左向右处理。 */ private Integer computeForAndThen(Integer num, Function<Integer, Integer> function1, Function<Integer, Integer> function2) { return function1.andThen(function2).apply(num); } /** * @param num1 * @param nuum2 * @param biFunction * @return * @desc 使用BiFunction */ private Integer computeForBiFunction(Integer num1, Integer num2, BiFunction<Integer, Integer, Integer> biFunction) { return biFunction.apply(num1, num2); } /** * @param num1 * @param num2 * @param biFunction * @param function * @return * @desc 使用BiFunction andThen方法 */ private Integer computeForBiFunctionAndThen(Integer num1, Integer num2, BiFunction<Integer, Integer, Integer> biFunction, Function<Integer, Integer> function) { return biFunction.andThen(function).apply(num1, num2); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)