Java Lambda表达式 匿名内部类 函数式接口(FunctionalInterface)
Java Lambda表达式
- 定义
- 背景
- 示例
- 匿名类实现
- Lambda表达式实现
- 对比匿名类和Lambda实现
- ==Lambda表达式(调用)说明==
- Lambda表达式的语法
- Java 1.8 新特性:函数式接口
- jdk 1.8 自带的函数式接口 (举例)
定义
参考Oracle官网:
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax
其他文章:https://blog.csdn.net/changlina_1989/article/details/111224385
背景
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an
interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In
these cases, you're usually trying to pass functionality as an argument to another method, such as what action should
be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method
argument, or code as data.The previous section, Anonymous Classes, shows you how to implement a base class without giving it a name.
Although this is often more concise than a named class, for classes with only one method, even an anonymous class
seems a bit excessive and cumbersome. Lambda expressions let you express instances of single-method classes
more compactly.
翻译 :
匿名类有个问题:就是如果匿名类的实现很简单,比如匿名类扩展的基类(如:接口)只有一个方法,那么使用匿名类就显得有点笨拙而不清晰了(简单说就是,使用起来不够简便!)。在这种情况下,总是希望可以将功能直接传给另外一个方法`,例如某人点击按钮后应该采取什么行动。
而lambda表达式刚好做到这一点,将功能作为方法的参数或者将代码作为数据。
匿名类可以通过实现一个基类而做到不需要名字。尽管匿名类的实现方式比较简洁但是在类只有一个方法时,匿名类还是有点不够简洁(因为Lambda表达式可以提供一种更简洁的方式)
综上:使用匿名类会比较简洁,使用Lambda表达式更简洁!!
示例
准备一个接口,并声明一个方法:用于匿名类及Lambda表达式
package com.xl.lambda;public interface PersonInterface {String thePerson(Person p);}
对应的实体Person
package com.xl.lambda;public class Person {private String name;private Integer age;private String sex;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}
}
编写测试时类
package com.xl.lambda;public class LambdaForTest {public static void main(String[] args) {LambdaForTest lambdaForTest = new LambdaForTest();//lambda表达式调用String lambdaResult = lambdaForTest.lambda("John Snow",26,pn -> pn.getAge() > 40 ? "中老年":"青少年");System.out.println("Lambda表达式实现:"+lambdaResult);//匿名类调用String anonymousResult = lambdaForTest.anonymous("John Snow",41);System.out.println("匿名类实现:"+anonymousResult);}/*** 使用lambda表达式实现* @param name* @param age* @param p* @return*/private String lambda(String name, Integer age, PersonInterface p) {Person pson = new Person();pson .setName(name);pson .setAge(age);return p.thePerson(pson );}/*** 使用匿名类实现* @param name * @param age* @param p* @return*/private String anonymous(String name, Integer age) {Person pson = new Person();pson .setName(name);pson .setAge(age);return new PersonInterface() {@Overridepublic String thePerson(Person p) {if (p.getAge() > 40) return "中老年";return "青少年";}}.thePerson(pn);}}
匿名类实现
匿名类可参见:https://blog.csdn.net/qq_29025955/article/details/129023869
方法实现
调用方法:
运行结果:
Lambda表达式实现
方法实现:
调用方法:
运行结果:
对比匿名类和Lambda实现
匿名类的方法实现需要
11 行代码;而用Lambda表达式只需要
4行代码,更加简洁!
但是,匿名类的方法实现可以由任意多个语句块或表达式组成,而Lambda表达式只能将实现(功能)放到一句表达式或一个语句块中。
匿名类省略了类的声明(包括类名字),变得简洁了; Lambda表达式不但省略了类的声明,连方法的声明(包括方法名)都省略了,直接将方法的实现(方法体)当作参数传递给另外一个方法!更简洁。
Lambda表达式(调用)说明
1、lambda表达式将功能(方法体)作为另外一个方法的参数或者将代码作为数据
支撑点:上面《背景》中有说明
2、lambda表达式只能是一句表达式或一个语句块
支撑点:
3、lambda表达式对应的接口只能有一个
抽象方法(没有实现 / 方法体),非default方法和静态方法
支撑点:
- JDK8开始,接口中方法可以有实现,前面加上default关键字即可,静态方法也可有实现,在接口PersonInterface 中添加如下方法,
package com.xl.lambda;public interface PersonInterface {String thePerson(Person p);default void another() {System.out.println("测试接口的default方法!");};static String third() {return "接口的静态方法!";}}
LambdaForTest测试类照样可以正常编译、运行!说明Lambda表达式不受影响。
- 但是,再加上一个抽象方法,Lambda编译就会报错:
根据错误提示
The target type of this expression must be a functional interface
表达式的类型必须是函数式接口。
什么是函数式的接口? : 只有一个抽象方法等待实现的接口。
本例中因为有两个抽象方法 thePerson(Person p) 和 theMan(),所以PersonInterface不是函数式接口。
4、Lambda表达式的使用:
编写方法时,声明一个函数式接口的参数,方法体调用
唯一的抽象方法;
调用方法时,直接写上抽象方法的实现/方法体。方法体的返回类型要与抽象方法的返回类型一致!!!
编写方法:
调用方法:
也可以将Lambda表达式单独定义出来
PersonInterface lambda = pn -> pn.getAge() > 40 ? "中老年":"青少年";
//lambda表达式调用2PersonInterface lambda = pn -> pn.getAge() > 40 ? "中老年":"青少年";String lambdaResult2 = lambdaForTest.lambda("John Snow",26,lambda);System.out.println("Lambda表达式实现2:"+lambdaResult2);
Lambda表达式的语法
参考 oracle:https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html#syntax
lambda表达式由以下3部分组成:
A comma-separated list of formal parameters enclosed in parentheses.
以逗号分隔的参数集合,放在圆括号里面。
- 没有参数时,直接写对圆即可。
- 只有一个参数时,圆括号可写可不写;
- 两个及以上参数时,需要圆括号;
The arrow token, ->
A body, which consists of a single expression or a statement block.
主体:由单个表达式或者单个语句块组成。
Java 1.8 新特性:函数式接口
参考1: https://zhuanlan.zhihu.com/p/531651771
参考2:Oracle 官网 java se 8 语言规范
什么是函数式接口? 官方定义:
A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract.
只有一个抽象方法的接口!代表了单个方法的契约。
jdk 1.8 自带的函数式接口 (举例)
这里以Function<T, R>为例
/** Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*********************/
package java.util.function;import java.util.Objects;/*** Represents a function that accepts one argument and produces a result.** <p>This is a <a href="package-summary.html">functional interface</a>* whose functional method is {@link #apply(Object)}.** @param <T> the type of the input to the function* @param <R> the type of the result of the function** @since 1.8*/
@FunctionalInterface
public interface Function<T, R> {/*** Applies this function to the given argument.** @param t the function argument* @return the function result*/R apply(T t);/*** Returns a composed function that first applies the {@code before}* function to its input, and then applies this function to the result.* If evaluation of either function throws an exception, it is relayed to* the caller of the composed function.** @param <V> the type of input to the {@code before} function, and to the* composed function* @param before the function to apply before this function is applied* @return a composed function that first applies the {@code before}* function and then applies this function* @throws NullPointerException if before is null** @see #andThen(Function)*/default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));}/*** Returns a composed function that first applies this function to* its input, and then applies the {@code after} function to the result.* If evaluation of either function throws an exception, it is relayed to* the caller of the composed function.** @param <V> the type of output of the {@code after} function, and of the* composed function* @param after the function to apply after this function is applied* @return a composed function that first applies this function and then* applies the {@code after} function* @throws NullPointerException if after is null** @see #compose(Function)*/default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));}/*** Returns a function that always returns its input argument.** @param <T> the type of the input and output objects to the function* @return a function that always returns its input argument*/static <T> Function<T, T> identity() {return t -> t;}
}
看完上面代码发现:
- 共有4个方法
- 2个default方法:带方法实现的(JDK1.8 新特性,接口的显示声明default的方法可以写实现)
- 1个静态方法:带方法实现的(JDK1.8 新特性,接口静态方法可以写实现)
- 1个抽象方法。
符合函数式接口的定义!但是,在接口声明的上方有个注解:@FunctionalInterface
关于@FunctionalInterface注解
参考 :Oracle 官网 java se 8 语言规范
The annotation type FunctionalInterface is used to indicate that an interface is meant to be a functional interface (§9.8). It facilitates early detection of inappropriate method declarations appearing in or inherited by an interface that is meant to be functional.
注解FunctionalInterface 用于表明当前接口是函数式接口,它有利于及早的检测当前接口是否满足函数式接口定义(只有一个抽象方法)。
It is a compile-time error if an interface declaration is annotated with @FunctionalInterface but is not, in fact, a functional interface.
如果一个接口加上了注解@FunctionalInterface,但是这个接口有不符合函数式接口的定义,那么就会报编译错误。
package com.xl.lambda;@FunctionalInterface
public interface TestInterface {String test();
// String test1();default String defaultImpl() {return "jdk1.8新特性:接口的default方法实现";}static String staticImpl() {return "jdk1.8新特性:接口的static方法实现";}}
如果,接口不符函数接口定义并且加上了@FunctionalInterface:如,有两个抽象方法呢?看下面的例子:
Because some interfaces are functional incidentally, it is not necessary or desirable that all declarations of functional interfaces be annotated with @FunctionalInterface.
如果一些接口碰巧是函数式接口,那么不用必须加@FunctionalInterface注解。
一句话总结:一个接口加了@FunctionalInterface就必须保证这个接口符合函数接口的定义,否则,会报编译错误。另外,函数式接口也可以不用加@FunctionalInterface注解,只要满足函数式接口的定义即可。
加@FunctionalInterface的好处:
如果你想写一个函数式接口,加上@FunctionalInterface注解后,在你写写错时:比如,写了两个及以上的抽象方法或一个抽象方法都没有, 会及时提示你(报编译错误!)
相关文章:

Java Lambda表达式 匿名内部类 函数式接口(FunctionalInterface)
Java Lambda表达式定义背景示例匿名类实现Lambda表达式实现对比匿名类和Lambda实现Lambda表达式(调用)说明Lambda表达式的语法Java 1.8 新特性:函数式接口jdk 1.8 自带的函数式接口 (举例)定义 参考Oracle官网&#x…...

javaEE 初阶 — 流量控制与拥塞控制
文章目录1. 流量控制2. 拥塞控制TCP 工作机制:确认应答机制 超时重传机制 连接管理机制 滑动窗口 1. 流量控制 流量控制是一种干扰发送的窗口大小的机制,滑动窗口,窗口越大,传输的效率就越高(一份时间,…...
HTML自主学习 - 2
一、表格 基本语法 <table><tr><td>单元格内容1</td><td>单元格内容2</td><td>单元格内容3</td></tr></table> 1、<table> </table>标签用于定义表格 2、<tr> </tr>标签用于定义表格的…...

【转载】通过HAL库实现MODBUS从机程序编写与调试-----STM32CubeMX操作篇
通过HAL库实现MODBUS从机程序编写与调试-----STM32CubeMX操作篇[【STM32】RS485 Modbus协议 采集传感器数据](https://blog.csdn.net/qq_33033059/article/details/106935583)基于STM32的ModbusRtu通信--ModbusRtu协议(一)基于STM32的ModbusRtu通信--终极Demo设计(二)STM32RS48…...

【C++】string类(上)
文章目录1.为什么要学习string类2.标准库中的string类1.string分类2.string类对象的常见构造1.string3. string类对象的容量操作1.size2.capacity3.reserve4.resize扩容初始化删除数据4. string类对象的修改操作1.push_back2.append3.operator1.为什么要学习string类 c语言的字…...
Java泛型
文章目录一、泛型介绍1. 背景2. 概念3. 好处二、泛型声明泛型类型符号泛型声明方式三、类型擦除1. 什么是类型擦除桥接方法2. 为何需要类型擦除3. 类型信息并未完全擦除四、泛型使用1. 泛型类2. 泛型接口3. 泛型方法五、泛型扩展1. 泛型的上下边界泛型的上边界泛型的下边界2. 泛…...

07 分布式事务Seata使用(2)
1、Seata是什么 Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式,为用户打造一站式的分布式解决方案。AT模式是阿里首推的模式,阿里云上有商用版本的GTS&#x…...
c++练习题5
5.在C语言中,程序运行期间,其值不能被改变的量叫 常量 。 6.符号常量是指用一个符号名代表一个常量。 7.整型常量和浮点型常量也称为 数值常量 ,它们有正负之分。 9.在C中,变量是 其值可以改变的量 。 …...

Python 高级编程之正则表达式(八)
文章目录一、概述二、正则表达式语法1)字符匹配2)字符集合3)定位符4)分组1、定义分组2、引用分组3、命名分组三、Python 的 re 模块1)re.match() 方法2)re.search() 方法3)re.match() 与 re.sea…...
pynrrd常用操作解析
目录依赖安装官方文档常用操作1. 读部分nrrd.read()nrrd.read_header()nrrd.read_data()2. 写部分nrrd.write()依赖安装 pip install pynrrd官方文档 https://pynrrd.readthedocs.io/en/stable/ 常用操作 1. 读部分 nrrd.read() nrrdpath "your nrrd file path"…...

数据结构:链表基础OJ练习+带头双向循环链表的实现
目录 一.leetcode剑指 Offer II 027. 回文链表 1.问题描述 2.问题分析与求解 (1) 快慢指针法定位链表的中间节点 (2) 将链表后半部分进行反转 附:递归法反转链表 (3) 双指针法判断链表是否回文 二.带头双向循环链表的实现 1.头文件 2.节点内存申请接口和链表初始化接口…...

计算机视觉方向地理空间遥感图像数据集汇总
文章目录1.DSTL卫星图像数据集/Kaggle竞赛2.Swimming Pool and Car Detection/Kaggle竞赛3.SpaceNet Challenge 3数据集4.RarePlanes数据集5.BigEarthNet数据集6.NWPU VHR-10数据集7.UC Merced Land-Use数据集8.Inria Aerial Image Labeling数据集9.RSOD数据集1.DSTL卫星图像数…...
信息系统项目管理师真题精选(一)
1.信息系统的( )决定了系统可以被外部环境识别,外部环境或者其他系统可以按照预定的方法使用系统的功能或者影响系统的行为。A.可嵌套性B.稳定性C.开放性D.健壮性2、在实际的生产环境中,( )能使底层物理硬件…...
信息系统项目管理师刷题知识点(持续更新)
主要记录自己在备考高项过程中知识点 信息系统项目管理师刷题知识点(按刷题顺序排列) 1.信息技术应用是信息化体系六要素中的龙头,是国家信息化建设的主阵地,集中体现了国家信息化建设的需求和效益。 2.原型化方法也称为快速原型法…...

RabbitMq及其他消息队列
消息队列中间价都有哪些 先进先出 Kafka、Pulsar、RocketMQ、RabbitMQ、NSQ、ActiveMQ Rabbitmq架构 消费推拉模式 客户端消费者获取消息的方式,Kafka和RocketMQ是通过长轮询Pull的方式拉取消息,RabbitMQ、Pulsar、NSQ都是通过Push的方式。 pull类型…...

Toolformer: Language Models Can Teach Themselves to Use Tools
展示了LM可以通过简单的API教自己使用外部工具,并实现两个世界的最佳效果。我们介绍了Toolformer,这是一个经过训练的模型,可以决定调用哪些API,何时调用,传递哪些参数,以及如何将结果最好地纳入未来的标记…...
悲观锁与乐观锁
何谓悲观锁与乐观锁 乐观锁对应于生活中乐观的人总是想着事情往好的方向发展,悲观锁对应于生活中悲观的人总是想着事情往坏的方向发展。这两种人各有优缺点,不能不以场景而定说一种人好于另外一种人。 悲观锁 总是假设最坏的情况,每次去拿数据…...

LeetCode 25. K 个一组翻转链表
原题链接 难度:hard\color{red}{hard}hard 题目描述 给你链表的头节点 headheadhead , kkk 个节点一组进行翻转,请你返回修改后的链表。 kkk 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 kkk 的整数倍…...
朗润国际期货招商:历次科技风头下巨头的博弈
历次科技风头下巨头的博弈 VR/AR、区块链、折叠屏、元宇宙、AIGC五轮科技风头下巨头们都进场了吗? VR/AR硬件 谷歌:2014年入局,推出AR眼镜 百度:未入局 京东:未入局 腾讯:传要开发 亚马逊࿱…...

配置中心Config
引入依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version></parent><properties><spring-cloud.version>Finchley.SR…...

springboot 百货中心供应链管理系统小程序
一、前言 随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,百货中心供应链管理系统被用户普遍使用,为方…...
AtCoder 第409场初级竞赛 A~E题解
A Conflict 【题目链接】 原题链接:A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串,只有在同时为 o 时输出 Yes 并结束程序,否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...

【JVM】- 内存结构
引言 JVM:Java Virtual Machine 定义:Java虚拟机,Java二进制字节码的运行环境好处: 一次编写,到处运行自动内存管理,垃圾回收的功能数组下标越界检查(会抛异常,不会覆盖到其他代码…...
条件运算符
C中的三目运算符(也称条件运算符,英文:ternary operator)是一种简洁的条件选择语句,语法如下: 条件表达式 ? 表达式1 : 表达式2• 如果“条件表达式”为true,则整个表达式的结果为“表达式1”…...
质量体系的重要
质量体系是为确保产品、服务或过程质量满足规定要求,由相互关联的要素构成的有机整体。其核心内容可归纳为以下五个方面: 🏛️ 一、组织架构与职责 质量体系明确组织内各部门、岗位的职责与权限,形成层级清晰的管理网络…...

Redis数据倾斜问题解决
Redis 数据倾斜问题解析与解决方案 什么是 Redis 数据倾斜 Redis 数据倾斜指的是在 Redis 集群中,部分节点存储的数据量或访问量远高于其他节点,导致这些节点负载过高,影响整体性能。 数据倾斜的主要表现 部分节点内存使用率远高于其他节…...
Rapidio门铃消息FIFO溢出机制
关于RapidIO门铃消息FIFO的溢出机制及其与中断抖动的关系,以下是深入解析: 门铃FIFO溢出的本质 在RapidIO系统中,门铃消息FIFO是硬件控制器内部的缓冲区,用于临时存储接收到的门铃消息(Doorbell Message)。…...

深度学习习题2
1.如果增加神经网络的宽度,精确度会增加到一个特定阈值后,便开始降低。造成这一现象的可能原因是什么? A、即使增加卷积核的数量,只有少部分的核会被用作预测 B、当卷积核数量增加时,神经网络的预测能力会降低 C、当卷…...

佰力博科技与您探讨热释电测量的几种方法
热释电的测量主要涉及热释电系数的测定,这是表征热释电材料性能的重要参数。热释电系数的测量方法主要包括静态法、动态法和积分电荷法。其中,积分电荷法最为常用,其原理是通过测量在电容器上积累的热释电电荷,从而确定热释电系数…...
Caliper 负载(Workload)详细解析
Caliper 负载(Workload)详细解析 负载(Workload)是 Caliper 性能测试的核心部分,它定义了测试期间要执行的具体合约调用行为和交易模式。下面我将全面深入地讲解负载的各个方面。 一、负载模块基本结构 一个典型的负载模块(如 workload.js)包含以下基本结构: use strict;/…...