位置: IT常识 - 正文

java8 (jdk 1.8) 新特性——Lambda

编辑:rootadmin
java8 (jdk 1.8) 新特性 ——初步认识 1. 什么是lambda? 目前已知的是,有个箭头 -> 说一大段官方话,也没有任何意义 我们直接看代码: 之前我们创建线程是这样的 Runnable runnable = new Runnable() { @Override public vo ...

推荐整理分享java8 (jdk 1.8) 新特性——Lambda,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

java8 (jdk 1.8) 新特性 ——初步认识

1. 什么是lambda?

目前已知的是,有个箭头->

说一大段官方话,也没有任何意义

我们直接看代码:

之前我们创建线程是这样的

Runnable runnable = new Runnable() { @Override public void run() { System.out.println("run。。。。。。"); } }; runnable.run();

用lambda:

Runnable run2 = () -> System.out.println("run。。。。。。");run2.run();

  

是不是感觉特别离谱,看不懂

别急,还有更离谱的

很常见的一个例子,比较两个整数的大小

之前是这样写的

Comparator<Integer> myCom = new Comparator<Integer>() {@Overridepublic int compare(Integer o1, Integer o2) {return Integer.compare(o1, o2);}};int compare = myCom.compare(12, 20);int compare1 = myCom.compare(20, 12);int compare2 = myCom.compare(20, 20);System.out.println(compare);System.out.println(compare1);System.out.println(compare2);}

  

用lambda:

Comparator<Integer> myCom = (o1, o2) -> Integer.compare(o1, o2);int compare = myCom.compare(12, 20);int compare1 = myCom.compare(20, 12);int compare2 = myCom.compare(20, 20);System.out.println(compare);System.out.println(compare1);System.out.println(compare2);

  甚至还可以这样 (这个是方法引用)

Comparator<Integer> myCom = Integer::compare;int compare = myCom.compare(12, 20);int compare1 = myCom.compare(20, 12);int compare2 = myCom.compare(20, 20);System.out.println(compare);System.out.println(compare1);System.out.println(compare2);

  

第一个数比第二个数

大 :返回 1

小:返回 -1

相等:返回 0

刚接触是不是黑人问号,这是什么玩意

很好,到这,你认识到了lambda 一个缺点,可阅读性差,优点 代码简洁

小结:看到 -> lambda 看到 : :方法引用

2. lamdba 语法

基本语法

1. 箭头操作符 -> 或者叫做lambda 操作符

2. 箭头操作符将lambda 表达式拆分成两部分

左侧:Lambda 表达式的参数列表

右侧:Lambda 表达式中所需执行的功能 , 即 Lambda 体

3.语法格式

语法格式1:无参数,无返回值

() -> system.out.println("Hello Lambda")

  

前边线程的那个例子就是

语法格式2:有一个参数 无返回值

(x)-> System.out.println(x);

  

若只有一个参数可以省略不写

x-> System.out.println(x);

  

java8 (jdk 1.8) 新特性——Lambda

之前的写法,没有使用lambda

Consumer<String> consumer = new Consumer<String>() {@Overridepublic void accept(String s) {System.out.println("输出的值:"+s);}};consumer.accept("今天不想emo了");

  

使用lambda

Consumer<String> consumer = s -> System.out.println("输出的值:"+s);consumer.accept("今天不想emo了");

 

语法格式3:有两个以上参数 ,并且lambda体有多条语句,有返回值

Comparator<Integer> myCom = (o1, o2) -> {System.out.println("其他语句");return Integer.compare(o1, o2);};

  

语法格式4 lambda体中只有一条语句,return 和 大括号都可以省略不写

Comparator<Integer> com =(x,y)-> Integer.compare(x,y);

  有没有发现所有的参数,都没有参数类型,之前我们写函数的时候可是要带上参数类型的

语法格式5 lambda表达式参数列表的数据类型可以省略不写,因为JVM编译器通过上下文编译推断出数据类型——类型推断

4.函数式接口

不管上面哪一种语法格式,lambda都是需要函数式接口的支持

函数式接口:接口中只有一个抽象方法的接口 称为函数式接口

可以使用一个注解@FunctionalInterface 修饰,可以检查是否是函数式接口

我们可以看看Runnable 接口 的源码

完成的接口类代码如下

/** Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms/package java.lang;/*** The <code>Runnable</code> interface should be implemented by any* class whose instances are intended to be executed by a thread. The* class must define a method of no arguments called <code>run</code>.* <p>* This interface is designed to provide a common protocol for objects that* wish to execute code while they are active. For example,* <code>Runnable</code> is implemented by class <code>Thread</code>.* Being active simply means that a thread has been started and has not* yet been stopped.* <p>* In addition, <code>Runnable</code> provides the means for a class to be* active while not subclassing <code>Thread</code>. A class that implements* <code>Runnable</code> can run without subclassing <code>Thread</code>* by instantiating a <code>Thread</code> instance and passing itself in* as the target. In most cases, the <code>Runnable</code> interface should* be used if you are only planning to override the <code>run()</code>* method and no other <code>Thread</code> methods.* This is important because classes should not be subclassed* unless the programmer intends on modifying or enhancing the fundamental* behavior of the class.** @author Arthur van Hoff* @see java.lang.Thread* @see java.util.concurrent.Callable* @since JDK1.0*/@FunctionalInterfacepublic interface Runnable {/*** When an object implementing interface <code>Runnable</code> is used* to create a thread, starting the thread causes the object's* <code>run</code> method to be called in that separately executing* thread.* <p>* The general contract of the method <code>run</code> is that it may* take any action whatsoever.** @see java.lang.Thread#run()*/public abstract void run();}

  

可以看到这里面就只有一个实现,有一个注解@FunctionalInterface ,说明它就是一个函数式接口,就可以进行lambda简写

来看 Comparator 也是一样 有@FunctionalInterface注解

这里可能就会有疑问,这边明明是有两个抽象方法,怎么是函数式接口呢?

别急 !!可以看到这边的注释, 说明这个equals 是 重写了超类 的 equals,本质上是对object 的重写,官方定义这样的抽象方法是不会被定义到 抽象接口数的 ,因此实际上只有一个抽象方法

我们自己可以试着定义 函数式接口,很简单

package com.test1.demo;@FunctionalInterfacepublic interface MyFunc {void method();}

  

好了,如果,写两个抽象接口会怎样?

可以看到注解报错了,所以注解用来校验作用就在这

重申一遍,函数式接口:接口中只有一个抽象方法的接口 称为函数式接口

注解只是拿来校验的,方便我们一看就知道这是一函数式接口类,不然还得数个数,验证一下是不是只有一个

现在我也重写equals ,可以看到并没有报错

5. java8 中内置四大核心函数式接口

Consumer<T> 消费型接口

//Consumer 消费型接口@Testpublic void testConsumer() {cosumer(1000, (m) -> System.out.println("星期一" + m));// 星期一1000.0}public void cosumer(double m, Consumer<Double> con) {con.accept(m);}

  

供给型接口 supplier<T>

@Testpublic void testSupplier() {List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100));for (Integer integer : numList) {System.out.println(integer); //100 以内10位随机整数}}// 需求:产生指定个数的整数放入集合中public List<Integer> getNumList(int num, Supplier<Integer> su) {List<Integer> list = new ArrayList<>();for (int i = 0; i < num; i++) {Integer integer = su.get();list.add(integer);}return list;}

  

Function<T,R> 函数型接口

@Testpublic void FunctioStr(){String c = getFunction("sbjikss", str -> str.toUpperCase());System.out.println(c); //SBJIKSSString sub = getFunction("sbjikss", str -> str.substring(2, 3));System.out.println(sub);//j}public String getFunction( String str, Function<String,String> f) {return f.apply(str);}

  

断言型接口 Predicate<T>

public void tetPre(){List<String> list = Arrays.asList("Hello","sss","xxxx","sjjss");List<String> list1 = filterStr(list, (pre) -> pre.length() > 3);for (String s : list1) {System.out.println(s); // Hello xxxx sjjss}}//需求:将满足条件字符串放入集合中public List<String> filterStr(List<String> old , Predicate<String> pre ){List<String> newList = new ArrayList<>();for (String str : old) {if (pre.test(str)){newList.add(str);}}return newList;}

  

感谢阅读!!!

本文链接地址:https://www.jiuchutong.com/zhishi/310492.html 转载请保留说明!

上一篇:织梦dedecms内容页上一页下一页链接调用教程(织梦cms要钱吗)

下一篇:python中Sobel算子如何使用(sobel算子 opencv)

  • 注册资本印花税减半征收政策
  • 计提并结转所得税费用的会计分录
  • 房地产企业土地出让金抵减销项税额
  • 一次性扣除固定资产政策案例
  • 固定资产接受捐赠的计入什么科目
  • 预付账款需要纳税申报吗
  • 借方是收入还是支出 正负号
  • 收到政府补贴现金流量表怎么填
  • 所得税审核费计入什么科目
  • 补提企业所得税会计分录
  • 购入但未开始使用的房产
  • 增值税普通发票税率
  • 税务局会限制每月交税吗
  • 地税印花税怎么算
  • 盈余公积什么时候调整
  • 土地增值税规定中央空调属于房屋建筑物吗
  • 小贷公司贷款的流程是什么
  • 代开普通发票要什么材料?
  • 清算财产分配会计分录
  • 公司购买理财的好处有哪些
  • 劳务报酬的个税计算方法
  • i912900k用什么主板好
  • win10好几个操作系统
  • 企业所得税分析方法
  • 年终奖是多发一个月工资吗
  • 在网上怎
  • win10您需要权限来执行删除
  • 酒店装修费用如何抵扣税
  • 增值税检查调整科目还在用吗
  • 合伙企业分红所得税如何少交
  • PHP:imagefontwidth()的用法_GD库图像处理函数
  • 银行本票出票金额大于账户余额
  • php pop
  • vscode+cmake
  • tsconfig baseurl
  • node最新版本
  • 终止pppoe会话
  • ps调整边缘在哪里快捷键
  • 固定资产减值损失属于营业外支出吗
  • 租入固定资产改良
  • 无票收入后又开票怎么办
  • 未达起征点销售额
  • 增值税进项发票勾选认证流程
  • 公司申请破产后债务谁来还
  • 个税申报月份错误怎么更改
  • 季度报税都是几月份
  • 实际报销金额
  • 实收资本增减变化为负数的原因
  • 增值税起征点有多少
  • 其他应收款对应的预算会计
  • 实收资本未到位
  • 母猪生小猪会计分录
  • 原材料入库损耗
  • 普通发票作废影响额度吗
  • 科目余额表上的销项税额贷方余额表示
  • 怎样计算加班工资?
  • sql server内存怎么设置合理
  • mysql的多表查询语句
  • 32位的win7和64位的win7要选择哪个呢?它们有什么区别?
  • xp系统没有鼠标自己乱跳
  • mac如何开启屏幕录制权限
  • 2021年win10累积更新
  • centos下安装windows
  • win7如何打开windows功能
  • windows中的hosts文件
  • Win10系统无法启动,如何修复
  • 车钥匙失灵了10秒教你快速解决
  • 二分法查找 js
  • python 变参
  • 使用jquery实现的项目
  • vue3.0diff
  • nodejs 加解密
  • [置顶] 转载自官方-unity5.0正式发布了,看看带来哪些重要的新特性!
  • 你真的了解摩羯座吗
  • 怎么利用python处理批量表格
  • 河南省地方税务局公告2017年第4号
  • 三方协议开票流程
  • 南京税务服务热线
  • 全国初中应用物理知识竞赛获奖名单
  • 交完契税,房产证没办,契税能退吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设