news 2026/3/22 6:54:29

Java基础-Lambda表达式、Java链式编程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java基础-Lambda表达式、Java链式编程

一、Lambda表达式

Lambda表达式的作用是什么?
简化函数式接口的匿名内部类。
什么样的接口是函数式接口?
只有一个抽象方法的接口就是函数式接口。

示例代码:目标:认识Lambda是如何简化匿名内部类的。

publicclassLambdaTest{publicstaticvoidmain(String[]args){Animala1=newAnimal(){@Overridepublicvoidrun(){System.out.println("老虎跑的贼快~~~~");}};a1.run();Swimmings1=newSwimming(){@Overridepublicvoidswim(){System.out.println("学生游泳贼溜~~~~");}};s1.swim();// Lambda可以简化函数式接口的匿名内部类// 可以上下文推断出真实的代码形式!Swimmings2=()->{System.out.println("老师游泳贼溜~~~~");};s2.swim();}}//接口@FunctionalInterfaceinterfaceSwimming{// 函数式接口中有且仅有一个抽象方法voidswim();}//抽象类abstractclassAnimal{publicabstractvoidrun();}

Lambda表达式的省略写法(进一步简化Lambda表达式的写法):
参数类型可以省略不写。
如果只有一个参数,参数类型可以省略,同时()也可以省略。
如果Lambda表达式中的方法体代码只有一行代码,可以省略大括号不写,同时要省略分号!此时,如果这行代码是return语句,也必须去掉return不写。

示例代码:

publicclassLambdaTest2{publicstaticvoidmain(String[]args){// 目标:掌握Lambda表达式简化常见函数式接口的匿名内部类double[]scores={99.5,90,59.5,78,98,55};// 需求:为每个分数加分10分。Arrays.setAll(scores,newIntToDoubleFunction(){@OverridepublicdoubleapplyAsDouble(intindex){returnscores[index]+10;}});Arrays.setAll(scores,index->scores[index]+10);System.out.println(Arrays.toString(scores));Student[]students=newStudent[4];// students = [null, null, null, null]// 0 1 2 3students[0]=newStudent("周芷若",21,'女',169.3);students[1]=newStudent("殷素素",38,'女',172.2);students[2]=newStudent("小昭",19,'女',168.5);students[3]=newStudent("张无忌",23,'男',183.5);Arrays.sort(students,newComparator<Student>(){@Overridepublicintcompare(Studento1,Studento2){returnDouble.compare(o1.getHeight(),o2.getHeight());}});Arrays.sort(students,(o1,o2)->Double.compare(o1.getHeight(),o2.getHeight()));System.out.println(Arrays.toString(students));}}
publicclassStudentimplementsComparable<Student>{privateStringname;privateintage;privatechargender;privatedoubleheight;publicStudent(){}publicStudent(Stringname,intage,chargender,doubleheight){this.name=name;this.age=age;this.gender=gender;this.height=height;}// 指定大小规则// 比较者:this// 被比较者:o@OverridepublicintcompareTo(Studento){/** * 官方规定: * 如果您认为左边大于右边,请返回正整数。 * 如果您认为左边小于右边,请返回负整数。 * 如果您认为左边等于右边,请返回0。 * 只要这么干,默认就是升序排序 *///return this.age - o.age; // 升序。returno.age-this.age;// 降序。}@OverridepublicStringtoString(){return"Student{"+"name='"+name+'\''+", age="+age+", gender="+gender+", height="+height+'}'+"\n";}}

二、Java链式编程

实现了链式调用模式,通过返回当前对象实现方法链式调用。

通过构造函数初始化初始值,支持链式操作。
示例代码:

publicclassChainableTest{privateintvalue;publicChainableTest(intinitialValue){this.value=initialValue;}//加publicChainableTestadd(intnum){this.value+=num;returnthis;}//乘法publicChainableTestmultiply(intnum){this.value*=num;returnthis;}//getvaluepublicintgetValue(){returnthis.value;}publicstaticvoidmain(String[]args){intresult=newChainableTest(5).add(3).multiply(2).getValue();System.out.println("Result: "+result);// 输出: Result: 16}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/20 0:37:34

蓝奏云直链解析终极指南:告别繁琐下载,一键获取高速链接

蓝奏云直链解析终极指南&#xff1a;告别繁琐下载&#xff0c;一键获取高速链接 【免费下载链接】LanzouAPI 蓝奏云直链&#xff0c;蓝奏api&#xff0c;蓝奏解析&#xff0c;蓝奏云解析API&#xff0c;蓝奏云带密码解析 项目地址: https://gitcode.com/gh_mirrors/la/Lanzou…

作者头像 李华
网站建设 2026/3/15 13:40:28

Windows苹果驱动一键安装:3分钟快速解决iPhone连接问题

Windows苹果驱动一键安装&#xff1a;3分钟快速解决iPhone连接问题 【免费下载链接】Apple-Mobile-Drivers-Installer Powershell script to easily install Apple USB and Mobile Device Ethernet (USB Tethering) drivers on Windows! 项目地址: https://gitcode.com/gh_mi…

作者头像 李华
网站建设 2026/3/17 11:11:09

云顶之弈自动化升级助手:解放双手的全新体验

云顶之弈自动化升级助手&#xff1a;解放双手的全新体验 【免费下载链接】LOL-Yun-Ding-Zhi-Yi 英雄联盟 云顶之弈 全自动挂机刷经验程序 外挂 脚本 ,下载慢可以到https://gitee.com/stringify/LOL-Yun-Ding-Zhi-Yi 项目地址: https://gitcode.com/gh_mirrors/lo/LOL-Yun-Din…

作者头像 李华
网站建设 2026/3/14 23:09:54

VoiceFixer音频修复完整教程:三步解决常见声音质量问题

VoiceFixer音频修复完整教程&#xff1a;三步解决常见声音质量问题 【免费下载链接】voicefixer General Speech Restoration 项目地址: https://gitcode.com/gh_mirrors/vo/voicefixer 你是否曾经因为录音中的杂音而错过重要信息&#xff1f;那些珍贵的语音记录被背景噪…

作者头像 李华
网站建设 2026/3/15 13:37:52

Windows苹果驱动一键搞定:让iPhone与电脑完美连接

Windows苹果驱动一键搞定&#xff1a;让iPhone与电脑完美连接 【免费下载链接】Apple-Mobile-Drivers-Installer Powershell script to easily install Apple USB and Mobile Device Ethernet (USB Tethering) drivers on Windows! 项目地址: https://gitcode.com/gh_mirrors…

作者头像 李华
网站建设 2026/3/17 16:21:18

Windows苹果驱动一键安装:快速解决iPhone连接难题

Windows苹果驱动一键安装&#xff1a;快速解决iPhone连接难题 【免费下载链接】Apple-Mobile-Drivers-Installer Powershell script to easily install Apple USB and Mobile Device Ethernet (USB Tethering) drivers on Windows! 项目地址: https://gitcode.com/gh_mirrors…

作者头像 李华