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

网站建设 北京/如何进行app推广

网站建设 北京,如何进行app推广,用jsp做网站的技术路线,景点与网站合作方案怎么做Java Lambda表达式定义背景示例匿名类实现Lambda表达式实现对比匿名类和Lambda实现Lambda表达式(调用)说明Lambda表达式的语法Java 1.8 新特性:函数式接口jdk 1.8 自带的函数式接口 (举例)定义 参考Oracle官网&#x…

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部分组成:

  1. A comma-separated list of formal parameters enclosed in parentheses.
    以逗号分隔的参数集合,放在圆括号里面。
  • 没有参数时,直接写对圆即可。
    在这里插入图片描述
  • 只有一个参数时,圆括号可写可不写;
    在这里插入图片描述
  • 两个及以上参数时,需要圆括号;
    在这里插入图片描述
  1. The arrow token, ->
  2. 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表达式&#xff08;调用&#xff09;说明Lambda表达式的语法Java 1.8 新特性&#xff1a;函数式接口jdk 1.8 自带的函数式接口 &#xff08;举例&#xff09;定义 参考Oracle官网&#x…...

javaEE 初阶 — 流量控制与拥塞控制

文章目录1. 流量控制2. 拥塞控制TCP 工作机制&#xff1a;确认应答机制 超时重传机制 连接管理机制 滑动窗口 1. 流量控制 流量控制是一种干扰发送的窗口大小的机制&#xff0c;滑动窗口&#xff0c;窗口越大&#xff0c;传输的效率就越高&#xff08;一份时间&#xff0c;…...

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 是一款开源的分布式事务解决方案&#xff0c;致力于提供高性能和简单易用的分布式事务服务。Seata 将为用户提供了 AT、TCC、SAGA 和 XA 事务模式&#xff0c;为用户打造一站式的分布式解决方案。AT模式是阿里首推的模式,阿里云上有商用版本的GTS&#x…...

c++练习题5

5.在C语言中&#xff0c;程序运行期间&#xff0c;其值不能被改变的量叫 常量 。 6.符号常量是指用一个符号名代表一个常量。 7&#xff0e;整型常量和浮点型常量也称为 数值常量 &#xff0c;它们有正负之分。 9&#xff0e;在C中&#xff0c;变量是 其值可以改变的量 。 …...

Python 高级编程之正则表达式(八)

文章目录一、概述二、正则表达式语法1&#xff09;字符匹配2&#xff09;字符集合3&#xff09;定位符4&#xff09;分组1、定义分组2、引用分组3、命名分组三、Python 的 re 模块1&#xff09;re.match() 方法2&#xff09;re.search() 方法3&#xff09;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.信息系统的&#xff08; &#xff09;决定了系统可以被外部环境识别&#xff0c;外部环境或者其他系统可以按照预定的方法使用系统的功能或者影响系统的行为。A.可嵌套性B.稳定性C.开放性D.健壮性2、在实际的生产环境中&#xff0c;&#xff08; &#xff09;能使底层物理硬件…...

信息系统项目管理师刷题知识点(持续更新)

主要记录自己在备考高项过程中知识点 信息系统项目管理师刷题知识点&#xff08;按刷题顺序排列&#xff09; 1.信息技术应用是信息化体系六要素中的龙头&#xff0c;是国家信息化建设的主阵地&#xff0c;集中体现了国家信息化建设的需求和效益。 2.原型化方法也称为快速原型法…...

RabbitMq及其他消息队列

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

Toolformer: Language Models Can Teach Themselves to Use Tools

展示了LM可以通过简单的API教自己使用外部工具&#xff0c;并实现两个世界的最佳效果。我们介绍了Toolformer&#xff0c;这是一个经过训练的模型&#xff0c;可以决定调用哪些API&#xff0c;何时调用&#xff0c;传递哪些参数&#xff0c;以及如何将结果最好地纳入未来的标记…...

悲观锁与乐观锁

何谓悲观锁与乐观锁 乐观锁对应于生活中乐观的人总是想着事情往好的方向发展&#xff0c;悲观锁对应于生活中悲观的人总是想着事情往坏的方向发展。这两种人各有优缺点&#xff0c;不能不以场景而定说一种人好于另外一种人。 悲观锁 总是假设最坏的情况&#xff0c;每次去拿数据…...

LeetCode 25. K 个一组翻转链表

原题链接 难度&#xff1a;hard\color{red}{hard}hard 题目描述 给你链表的头节点 headheadhead &#xff0c; kkk 个节点一组进行翻转&#xff0c;请你返回修改后的链表。 kkk 是一个正整数&#xff0c;它的值小于或等于链表的长度。如果节点总数不是 kkk 的整数倍&#xf…...

朗润国际期货招商:历次科技风头下巨头的博弈

历次科技风头下巨头的博弈 VR/AR、区块链、折叠屏、元宇宙、AIGC五轮科技风头下巨头们都进场了吗&#xff1f; VR/AR硬件 谷歌&#xff1a;2014年入局&#xff0c;推出AR眼镜 百度&#xff1a;未入局 京东&#xff1a;未入局 腾讯&#xff1a;传要开发 亚马逊&#xff1…...

配置中心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…...

【原创】java+jsp+servlet学生信息管理系统(jdbc+ajax+filter+cookie+分页)

一直想写一个比较基础的JavaWeb项目&#xff0c;然后综合各种技术&#xff0c;方便Java入门者进行学习。学生信息管理系统大家一般接触的比较多&#xff0c;那么就以这个为例来写一个基础项目吧。 需求分析&#xff1a; 使用jspservletmysql开发的学生信息管理系统&#xff0…...

链表题目总结 -- 回文链表

目录一. 从中心开始找最大的回文字符串1. 思路简述2. 代码3. 总结二. 判断是否为回文字符串1. 思路简述2. 代码3.总结三. 判断是否是回文链表1. 思路简述2. 代码3. 总结4. 优化解法一. 从中心开始找最大的回文字符串 题目链接&#xff1a;没有。给定一个字符串s&#xff0c;从…...

JAVA集合之List >> Arraylist/LinkedList/Vector结构

在Java开发过程中&#xff0c;可能经常会使用到List作为集合来使用&#xff0c;List是一个接口承于Collection的接口&#xff0c;表示着有序的列表。而我们要讨论的是它下面的实现类Arraylist/LinkedList/Vector的数据结构及区别。 ArrayList ArrayList&#xff1a;底层为数组…...

Linux多进程开发

一、进程概述 1、程序和进程 程序是包含一系列信息的文件&#xff0c;这些信息描述了如何在运行时创建一个进程&#xff1a; 二进制格式标识&#xff1a;每个程序文件都包含用于描述可执行文件格式的元信息。内核利用此信息来解释文件中的其他信息。&#xff08;ELF可执行连…...

三维重建小有基础入门之特征点检测基础

前言&#xff1a;本文将从此篇开始&#xff0c;记录自己从普通CVer入门三维重建的学习过程&#xff0c;可能过程比较坎坷&#xff0c;都在摸索阶段&#xff0c;但争取每次学习都能进一步&#xff0c;提高自己的能力&#xff0c;同时&#xff0c;每篇文章都会按情况相应地推出B站…...

基于node.js+vue+mysql考研辅导学习打卡交流网站系统vscode

语言 node.js 框架&#xff1a;Express 前端:Vue.js 数据库&#xff1a;mysql 数据库工具&#xff1a;Navicat 开发软件&#xff1a;VScode 主要功能包括管理员&#xff1a;首页、个人中心、用户管理、每日打卡管理、考研学校管理、考研专业管理、直通车管理、学习教材管理、…...

【C++、数据结构】封装unordered_map和unordered_set(用哈希桶实现)

文章目录&#x1f4d6; 前言1. 复用同一个哈希桶⚡1.1 &#x1f300;修改后结点的定义1.2 &#x1f300;两个容器各自模板参数类型&#xff1a;2. 改造之后的哈希桶⛳3. 哈希桶的迭代器&#x1f525;3.1 &#x1f4a5;哈希桶的begin&#xff08;&#xff09;和 end&#xff08;…...

StratoVirt 的 vCPU 拓扑(SMP)

CPU 拓扑用来表示 CPU 在硬件层面的组合方式&#xff0c;本文主要讲解 CPU 拓扑中的 SMP&#xff08;Symmetric Multi-Processor&#xff0c;对称多处理器系统&#xff09;架构&#xff0c;CPU 拓扑还包括其他信息&#xff0c;比如&#xff1a;cache 等&#xff0c;这些部分会在…...

现在直播大部分都是RTMP RTMP VS RTC

一 RTMP 抓了下抖音直播的包&#xff0c;windows端&#xff0c;走的TCP&#xff0c;加密了&#xff0c;估计还是RTMP。 我以为直播带货&#xff0c;都是RTC了。 快手直播也是TCP&#xff0c;地址用了IPV6。 淘宝直播也是。现在大部分直播都是RTMP。 只有视频会议走的RTC。…...

【Unity实战100例】Unity循环UI界面切换卡片功能

目录 ​编辑 一:制作UI界面 二:代码逻辑 1.定义基础变量...