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

JDK8之四大核心函数式接口

四大核心函数式接口

Consumer 接口

  1. 接口说明
    Consumer 接口是消费性接口,无返回值。Java8 中对 Consumer 的定义如下所示。
/** * Represents an operation that accepts a single input argument and returns no * result. Unlike most other functional interfaces, {@code Consumer} is expected * to operate via side-effects. * * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is {@link #accept(Object)}. * * @param <T> the type of the input to the operation * * @since 1.8 */@FunctionalInterfacepublicinterfaceConsumer<T>{/** * Performs this operation on the given argument. * * @param t the input argument */voidaccept(Tt);/** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */defaultConsumer<T>andThen(Consumer<?superT>after){Objects.requireNonNull(after);return(Tt)->{accept(t);after.accept(t);};}

使用说明

publicvoidhandlerConsumer(Integernumber,Consumer<Integer>consumer){consumer.accept(number);}@Testpublicvoidtest1(){this.handlerConsumer(10000,(i)->System.out.println(i));}

Supplier 接口

  1. 接口说明
    Supplier 接口是供给型接口,有返回值,Java8 中对 Supplier 接口的定义如下所示。
@FunctionalInterfacepublicinterfaceSupplier<T>{Tget();}

使用示例

publicList<Integer>getNumberList(intnum,Supplier<Integer>supplier){List<Integer>list=newArrayList<>();for(inti=0;i<num;i++){list.add(supplier.get())}returnlist;}@Testpublicvoidtest2(){List<Integer>numberList=this.getNumberList(10,()->newRandom().nextInt(100));numberList.stream().forEach(System.out::println);}

Function 接口

  1. 接口说明
    Function 接口是函数型接口,有返回值,Java8 中对 Function 接口的定义如下所示。
@FunctionalInterfacepublicinterfaceFunction<T,R>{Rapply(Tt);default<V>Function<V,R>compose(Function<?superV,?extendsT>before){Objects.requireNonNull(before);return(Vv)->apply(before.apply(v));}default<V>Function<T,V>andThen(Function<?superR,?extendsV>after){Objects.requireNonNull(after);return(Tt)->after.apply(apply(t));}static<T>Function<T,T>identity(){returnt->t;}}

使用示例

publicStringhandlerString(Stringstr,Function<String,String>func){returnfunc.apply(str);}@Testpublicvoidtest3(){Stringstr=this.handlerString("binghe",(s)->s.toUpperCase());System.out.println(str);}

Predicate 接口

  1. 接口说明
    Predicate 接口是断言型接口,返回值类型为 boolean,Java8 中对 Predicate 接口的定义如下所示。
@FunctionalInterfacepublicinterfacePredicate<T>{booleantest(Tt);defaultPredicate<T>and(Predicate<?superT>other){Objects.requireNonNull(other);return(t)->test(t)&&other.test(t);}defaultPredicate<T>negate(){return(t)->!test(t);}defaultPredicate<T>or(Predicate<?superT>other){Objects.requireNonNull(other);return(t)->test(t)||other.test(t);}static<T>Predicate<T>isEqual(ObjecttargetRef){return(null==targetRef)?Objects::isNull:object->targetRef.equals(object);}}

使用示例

publicList<String>filterString(List<String>list,Predicate<String>predicate){List<String>strList=newArrayList<>();for(Stringstr:list){if(predicate.test(str)){strList.add(str);}}returnstrList;}@Testpublicvoidtest4(){List<String>list=Arrays.asList("Hello","Lambda","binghe","lyz","World");List<String>strList=this.filterString(list,(s)->s.length()>=5);strList.stream().forEach(System.out::println);}
http://www.jsqmd.com/news/531411/

相关文章:

  • Anything-v5+Pixel Fashion Atelier效果展示:像素方块世界里的高定皮装美学
  • 医疗AI终极突破:Baichuan-M3超越GPT-5.2解密
  • CC2530在IAR中配置生成hex文件的完整流程及常见错误排查
  • 锂电池不同倍率充放电特性分析与SOC估算优化
  • vLLM-v0.17.1效果展示:Qwen2-VL多模态模型vLLM适配初步成果
  • Z-Image-Turbo_Sugar脸部Lora一文详解:LoRA微调原理、基础镜像适配与优化要点
  • YOLO X Layout Web界面操作指南:上传图片调整阈值,结果可视化
  • DolphinScheduler实战:如何用YAML+Go打造高效离线数据治理平台(附完整配置)
  • 别再只会print(‘Hello World‘)了!用Python Emoji模块给你的命令行程序加点表情包
  • PyTorch 2.8镜像惊艳效果:RTX 4090D实测Wan2.2-I2V图片转视频流畅度测试
  • Stata数据分析:从描述统计到散点图,一条命令搞定探索性分析(附完整代码)
  • 通义千问3-Reranker-0.6B开源可部署:Apache 2.0许可下自主可控检索升级
  • Vulnhub靶机实战:MERCURY提权全记录(附环境配置避坑指南)
  • 计算机毕业设计springboot银饰网上商城设计与实现 基于SpringBoot的银饰品在线销售平台设计与实现 SpringBoot框架下银饰网络销售系统的设计与开发
  • SeqGPT-560m指令理解能力实测:任务-输入-输出Prompt结构有效性验证
  • SEO_从0到1搭建可持续流量的SEO体系介绍
  • 原神祈愿记录导出工具:从数据捕获到可视化分析的全流程解决方案
  • 别再全表扫描了!GaussDB分区表实战:用时间戳分区让IoT数据查询快10倍
  • 【仅限前500名开发者】Python AI内存泄漏检测SDK免费开放:内置17个LLM服务典型泄漏模式指纹库
  • 别再傻傻仿真整个阵列了!CST微波工作室教你用周期边界快速搞定FSS单元仿真
  • 联想ideapad700-15ISK加装M.2固态实战:三星970EVO安装与双系统迁移避坑指南
  • Wan2.2-I2V-A14B惊艳生成:海鸥翅膀扇动频率与空气动力学模型匹配
  • 告别Mac!用香蕉云编在线搞定iOS证书(.p12)和描述文件,HBuilderX打包必备
  • Qwen3-1.7B快速上手:CSDN镜像开箱即用,无需自己装环境
  • payload-dumper-go:Android OTA包高效解压工具,释放系统镜像价值
  • Rocky Linux 9.4上iRedMail 1.6.8邮件系统保姆级安装指南(含SOGo避坑技巧)
  • C# Random 随机数实战技巧与高级应用
  • 上海交大首次发布完全开源的搜索智能体
  • Phi-4-Reasoning-Vision部署教程:双卡4090环境下的15B模型梯度检查点优化
  • RVC-WebUI终极指南:5分钟掌握AI语音转换技术