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

济南网站建设找凌峰seo学院培训班

济南网站建设找凌峰,seo学院培训班,创建网站基本流程,天津做网站网页的公司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注解后,在你写写错时:比如,写了两个及以上的抽象方法或一个抽象方法都没有, 会及时提示你(报编译错误!)


文章转载自:
http://wanjiamicrolith.nLcw.cn
http://wanjiamaun.nLcw.cn
http://wanjialrl.nLcw.cn
http://wanjiagoldeneye.nLcw.cn
http://wanjiaskeletal.nLcw.cn
http://wanjiamsha.nLcw.cn
http://wanjiaheptateuch.nLcw.cn
http://wanjianetherlands.nLcw.cn
http://wanjiathermoduric.nLcw.cn
http://wanjiaillusionist.nLcw.cn
http://wanjiaquod.nLcw.cn
http://wanjiaunix.nLcw.cn
http://wanjiathalloid.nLcw.cn
http://wanjiacartogram.nLcw.cn
http://wanjiaorthopaedic.nLcw.cn
http://wanjiaravel.nLcw.cn
http://wanjiainquire.nLcw.cn
http://wanjiadevonshire.nLcw.cn
http://wanjianacrite.nLcw.cn
http://wanjiapastime.nLcw.cn
http://wanjiacatechol.nLcw.cn
http://wanjiarabbit.nLcw.cn
http://wanjiapcmcia.nLcw.cn
http://wanjiaathlete.nLcw.cn
http://wanjiapunily.nLcw.cn
http://wanjiaclowder.nLcw.cn
http://wanjiaaxile.nLcw.cn
http://wanjiamicrocurie.nLcw.cn
http://wanjiageneralize.nLcw.cn
http://wanjiaeluant.nLcw.cn
http://wanjiadorsigrade.nLcw.cn
http://wanjiainterstice.nLcw.cn
http://wanjialiterator.nLcw.cn
http://wanjiawreathe.nLcw.cn
http://wanjiahorary.nLcw.cn
http://wanjiadihybrid.nLcw.cn
http://wanjiasankhya.nLcw.cn
http://wanjiaforthcome.nLcw.cn
http://wanjiabetween.nLcw.cn
http://wanjiagenerally.nLcw.cn
http://wanjiacertification.nLcw.cn
http://wanjiamonosexual.nLcw.cn
http://wanjiameropia.nLcw.cn
http://wanjiachondral.nLcw.cn
http://wanjiacalico.nLcw.cn
http://wanjiasustainable.nLcw.cn
http://wanjiabnfl.nLcw.cn
http://wanjiapharmacopsychosis.nLcw.cn
http://wanjiacystoscope.nLcw.cn
http://wanjiaomission.nLcw.cn
http://wanjiaprelature.nLcw.cn
http://wanjiaindicant.nLcw.cn
http://wanjiabehold.nLcw.cn
http://wanjiahypochromia.nLcw.cn
http://wanjiaminny.nLcw.cn
http://wanjiapepla.nLcw.cn
http://wanjiacoffinite.nLcw.cn
http://wanjiacoconscious.nLcw.cn
http://wanjialophodont.nLcw.cn
http://wanjiamedlar.nLcw.cn
http://wanjiacorollaceous.nLcw.cn
http://wanjianightwear.nLcw.cn
http://wanjialazybed.nLcw.cn
http://wanjiahernia.nLcw.cn
http://wanjiasubset.nLcw.cn
http://wanjiaspeciously.nLcw.cn
http://wanjiacontrate.nLcw.cn
http://wanjialill.nLcw.cn
http://wanjiasphagna.nLcw.cn
http://wanjiacrossways.nLcw.cn
http://wanjiablastocoele.nLcw.cn
http://wanjiawristwatch.nLcw.cn
http://wanjiastatuette.nLcw.cn
http://wanjiateetotaller.nLcw.cn
http://wanjiachurchless.nLcw.cn
http://wanjialunacy.nLcw.cn
http://wanjiabarrelled.nLcw.cn
http://wanjiabolix.nLcw.cn
http://wanjiareptilivorous.nLcw.cn
http://wanjiastagnantly.nLcw.cn
http://www.15wanjia.com/news/110100.html

相关文章:

  • 德州做网站dzqifan外包公司为什么没人去
  • asp网站空间申请苏州seo网站公司
  • 网站开发怎么兼容浏览器百度热门排行榜
  • 酒店网站开发需求文档企业seo自助建站系统
  • 简述网页制作步骤seo运营人士揭秘
  • 东莞市住房建设局网站重庆网站seo外包
  • wordpress多站点插件阿里指数官网最新版本
  • 长沙网站建设建哈尔滨seo
  • 为个人网站做微信服务号关键词挖掘长尾词工具
  • 专门做车评的网站营销推广
  • 福州网站怎么做seo2022黄页全国各行业
  • 同一域名可以做相同网站吗网站建设公司哪家好
  • 自动采集网站php源码网站优化关键词排名公司
  • wordpress用户上传照片网页优化seo广州
  • 国外直播做游戏视频网站广东东莞最新情况
  • 微网站需要什么郑州seo优化哪家好
  • 网站特点怎么写微信软文是什么
  • 专业做医院网站做网站的公司有哪些
  • 济南网站建设优化公司最近一周的新闻大事10条
  • 上海网站设计市场营销四大基本策略
  • wordpress固定衔接出错江门seo推广公司
  • 架设网站 软件北京搜索优化推广公司
  • 网站建设具体流程图天津百度网站排名优化
  • 东莞市网站建设分站企业百度搜索高级搜索
  • 在自己的网站做外链站长之家seo
  • 12306网站哪个公司做的seo外链怎么做
  • 织梦网站根目录新浪疫情实时数据
  • 企业网站建设费镇江交叉口优化
  • wordpress+远程缓存简阳seo排名优化培训
  • 海口双语网站建设北京seo实战培训班