尝试理解FunctionalInterface

news/2025/10/21 17:57:34/文章来源:https://www.cnblogs.com/niutouzdq/p/19156066
尝试理解FunctionalInterface

在网上找了例子,自己改改,写写,注释注释。下面把我对functionalinterface里面的function的理解过程贴上来。

import java.util.Objects;

@FunctionalInterface
public interface FunctionC<T,R>{
/**
* function of interface FunctionC<T,R>
*/
R apply(T t);

/**
* using lambda to describe method compose
* andThen = (T t) -> after.apply(apply(t));
*
* default: no need to realized
*
* <V>: declare an Object V
*
* FunctionC<T,V>: declare an Interface FunctionC but using<T,V>,
* this means Interface FunctionC<T,V>{ V apply(T t);}
*
* andThen: method name
*
* FunctionC<? super R, ? extends V> after: declare the param type
* which is an realized object named 'after' that is of Interface FunctionC but using <? super R, ? extends V>,
* and that means Interface FunctionC<? super R, ? extends V>{ ? extends V apply(? super R r);}
* which almostly equals Interface FunctionC<R, V>{ V apply(R r);}
*
* by the way, the declaration of this interface is FunctionC<T,R>{ R apply(T t);}
* so we have declared three interface with type-relationship-table
*<table>
*<tr><th>InterfaceGenericType</th><th>ReturnType</th><th>ParamType</th><th>IsEntrance</th></tr>
*<tr><td>FunctionC<T,R></td><td>R</td><td>T</td><td>No</td></tr>
*<tr><td>FunctionC<T,V></td><td>V</td><td>T</td><td>Yes</td></tr>
*<tr><td>FunctionC<R,V></td><td>V</td><td>R</td><td>No</td></tr>
     *</table>
     *
* then if we want to get V by using T as param, we could use this type-relationship-table
* we send T to function apply(T) then we get R, and we sent R to function apply(R) we get V.
* so we get the V by using function ? apply(?), and the type-relationship-table.
* so we put all we need into the declaretion statement, down here.
*/
default <V> FunctionC<T,V> andThen(FunctionC <? super R, ? extends V> after){
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}

/**
* using lambda to describe method compose
* compose = (V v) -> apply(before.apply(v))
*
* default: no need to realized
*
* <V>: declare an Object V
*
* FunctionC<V,R>: declare an Interface FunctionC but using<V,R>,
* this means Interface FunctionC<V,R>{ R apply(V v);}
*
* compose: method name
*
* FunctionC <? super V, ? extends T> before: declare the param type
* which is an realized object named 'before' that is of Interface FunctionC but using <? super V, ? extends T>,
* and that means Interface FunctionC<? super V, ? extends T>{ ? extends T apply(? super V v);}
* which almostly equals Interface FunctionC<V, T>{ T apply(V v);}
*
* by the way, the declaration of this interface is FunctionC<T,R>{ R apply(T t);}
* so we have declared three interface with type-relationship-table
*<table>
*<tr><th>InterfaceGenericType</th><th>ReturnType</th><th>ParamType</th><th>IsEntrance</th></tr>
*<tr><td>FunctionC<T,R></td><td>R</td><td>T</td><td>No</td></tr>
*<tr><td>FunctionC<V,R></td><td>R</td><td>V</td><td>Yes</td></tr>
*<tr><td>FunctionC<V,T></td><td>T</td><td>V</td><td>No</td></tr>
*</table>
*
* then if we want to get R by using V as param, we could use this type-relationship-table
* we send V to function apply(V) then we get T, and we sent T to function apply(T) we get R.
* so we get the R by using function ? apply(?), and the type-relationship-table.
* so we put all we need into the declaretion statement, down here.
*/
default <V> FunctionC<V,R> compose(FunctionC <? super V, ? extends T> before){
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
}

public class AndThenTest{
public static void main(String args[]){
FunctionC<Integer, Integer> func1 = num -> num+1;
FunctionC<Integer, Integer> func2 = num -> num*10;
FunctionC<Integer, Integer> func3 = num -> num-5;
final Integer i = 10;
System.out.println("========================function.andThen=========================");
System.out.println("while num is " + i );
System.out.println(", num+1 is: " + func1.apply(i));
System.out.println(", and (num+1).andThen(num*10).andThen(num-5) is: " +
func1.andThen(func2).andThen(func3).apply(i));
FunctionC<Integer, Integer> func4 = num -> num;
System.out.println("use lambda num.andThen(num -> num*10).andThen(num -> num-5) is: " +
func4.andThen(num -> num*10).andThen(num -> num-5).apply(i));

System.out.println("========================function.compose=========================");
System.out.println("while num is " + i );
System.out.println(", num+1 is: " + func1.apply(i));
System.out.println(", and (num+1).compose.(num*10).compose(num-5) is: " +
func1.compose(func2).compose(func3).apply(i));
}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/942527.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

2025 年防淹门源头厂家最新推荐排行榜权威发布,含地铁 / 防洪 / 地下通道专用款,15 项专利 + 央视报道品牌领衔

引言 当前全球气候变暖加剧,极端降雨引发的城市内涝灾害频发,地铁、地下车库、江河堤坝等关键区域对防淹门的需求愈发迫切。但防淹门市场乱象丛生,部分厂家用劣质材料生产的产品抗压抗折弯性能不足,还有厂家因缺乏…

一文带你掌握Visual Studio中集成的git功能

前言 Visual Studio中深度集成了git功能,可以很方便的进行源代码版本控制功能。 大部分日常的操作我们可以通过界面来完成,这样就省去了输入git命令的时间,也可以不用记很多参数。 但这毕竟是辅助工具,掌握常用的g…

2025年手持光谱仪厂家权威推荐榜:光谱分析仪/便携式光谱仪、矿石元素分析、合金金属不锈钢铝合金、贵金属三元催化检测设备精选

2025年手持光谱仪厂家权威推荐榜:光谱分析仪/便携式光谱仪、矿石元素分析、合金金属不锈钢铝合金、贵金属三元催化检测设备精选 行业技术发展现状与趋势 手持光谱仪作为现代工业检测领域的重要工具,其技术迭代速度正…

高级程序语言设计课程第二次作业

这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/gjyycx 这个作业要求在哪里:https://www.cnblogs.com/zhuyuchen818/p/19155507 学号: 102500325 姓名:李志鹏 编写并运行书本第3章3.11 编程练习题目中的…

2025年定型机厂家推荐排行榜,拉幅定型机/门富士,节能定型机/余热回收,废气回收/烟气回收,智能排风/双层定型机源头企业深度解析

2025年定型机厂家推荐排行榜,拉幅定型机/门富士,节能定型机/余热回收,废气回收/烟气回收,智能排风/双层定型机源头企业深度解析 在纺织印染行业持续转型升级的背景下,定型机作为后整理工序的核心装备,其技术水平…

iOS 混淆实战 多工具组合完成 IPA 混淆、加固与工程化落地(iOS混淆|IPA加固|无源码混淆|Ipa Guard|Swift Shield)

本文以对话与实战流程还原 iOS 混淆落地:用 MobSF/class-dump 做静态发现,Swift Shield(可选)做源码混淆,Ipa Guard 做成品 IPA 混淆,Fastlane/Jenkins 自动化流水线,Frida/Hopper 动态验证,映射表由 KMS 管理…

单提交智能评审上线!用云效精准定位复杂 MR 代码问题

随着代码评审进入智能化时代,AI 已成为提升 Code Review 效率与代码质量的重要助手。但当一次合并请求(MR)包含大量提交或巨量变更时,把所有 diff 一次性交给 AI 审查,容易导致判断失真、遗漏细节或误解改动意图。…

ubuntu安装nvidia驱动 - Leonardo

一、驱动安装 1.驱动安装 方法一:使用官方 NVIDIA 仓库(推荐) sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt update ubuntu-drivers devices sudo ubuntu-drivers autoinstall sudo reboot方法二:使…

十五、深入理解 SELinux - 指南

十五、深入理解 SELinux - 指南2025-10-21 17:49 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !importa…

2025 年少儿英语品牌口碑排行榜最新发布:欧美外教 + 原版教材甄选,含最新推荐及靠谱选择指南

引言 随着家长对少儿英语教育重视程度不断提升,市场上各类少儿英语品牌数量激增,但质量却良莠不齐,给家长选择带来极大困扰。部分品牌存在外教非母语、教龄短,难以营造纯正语言环境;教材脱离国际标准与国内应试需…

GitLab小坑:remote: GitLab: You are not allowed to create protected branches on this project.

git -c credential.helper= -c core.quotepath=false -c log.showSignature=false push --progress --porcelain origin refs/heads/release-xxx-0.4:release-abc这个本地提交push到新分支时,idea生成的git命令。然而…

使用StockTV数据源对接美国股票数据

🎯 核心功能与优势 StockTV API 支持包括美国在内的10多个国家的股票市场数据。其核心优势在于:​​实时数据推送​​:通过 WebSocket 提供毫秒级延迟的实时行情推送。 ​​数据覆盖面广​​:除了股票,还覆盖指数…

构筑平安工地新防线:国标GB28181算法算力平台EasyGBS的平安工地解决方案

构筑平安工地新防线:国标GB28181算法算力平台EasyGBS的平安工地解决方案引言:平安工地的时代需求与技术痛点 随着我国城市化进程的不断加速,建筑工地的规模与复杂性日益提升。然而,传统的工地安全管理模式高度依赖…

2025 年国内控制柜生产厂家最新推荐排行榜:聚焦技术实力与 OEM 服务能力专业测评解析

引言 在工业自动化加速渗透的当下,控制柜作为设备运行的 “神经中枢”,其性能直接关系到企业生产效率、安全运营与节能成效。当前市场上,控制柜厂家数量繁杂,部分企业存在技术滞后、定制能力薄弱、售后响应缓慢等问…

服务器运维(一)linux进程指令大全——东方仙盟筑基期 - 指南

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

2025年滑石粉厂家推荐排行榜,纳米级滑石粉,工业级滑石粉,黑色滑石粉,高白滑石粉,化妆品级滑石粉,食品级滑石粉,表面改性滑石粉,大片径比滑石粉,低收缩率滑石粉,高填充母粒滑石粉

2025年滑石粉厂家推荐排行榜:从纳米级到高填充母粒的全方位指南 在现代化工材料领域,滑石粉作为一种重要的功能性填料,其应用范围已从传统的塑料、涂料行业扩展到高端化妆品、食品加工等新兴领域。随着产业升级和技…

自动化智能体与测试用例生成

关注 霍格沃兹测试学院公众号,回复「资料」, 领取人工智能测试开发技术合集 每天重复写着相似的测试用例,翻阅上百页的需求文档寻找测试点,为了一个边界值绞尽脑汁……这是不是你的日常? 好消息是,AI自动化的时代…

设置某些网站不走代理

场景:公司内网访问的网站、本地启动的web应用不能走代理。 打开setting/Bypass Domain/IpNet,编辑配置文件该配置保存即生效,不需要重启。

2025年粘度计厂家权威推荐榜:在线旋转振动/实验室反应釜管线在线粘度计,专业选型指南与技术创新深度解析

2025年粘度计厂家权威推荐榜:在线旋转振动/实验室反应釜管线在线粘度计,专业选型指南与技术创新深度解析 在工业过程控制和实验室分析领域,粘度测量技术作为流体特性监测的关键环节,正经历着从离线抽样到在线连续的…

jdk8安装

jdk8安装安装jdk 1、安装目录:C:\Program Files\Java\jdk1.8.0_181 2、配置系统变量3、配置path变量4、cmd中java -version