Functional Interface
.什么是功能接口?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@功能接口 上市 接口 可装饰的 { //一种抽象方法 虚空 decorateWithCurtains(); //默认方法 默认 虚空 decorateWithPaints() { 系统.出.打印(“用油漆装饰”); } // java.lang.Object的重写方法 @覆写 上市 整型 hashCode(); } |
- 爪哇.lang.Runnable
- 爪哇.util.concurrent.Callable
- 爪哇.awt.event.ActionListener
- 爪哇.util.Comparator
为什么使用Lambda表达式?
让’s说您需要按电影名称对电影列表进行排序。
电影.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
包 组织.Arpit.爪哇2blog; 上市 类 电影 { 串 电影Name; 长 持续时间; 上市 电影(串 电影Name, 长 持续时间) { 超(); 这个.电影Name = 电影Name; 这个.持续时间 = 持续时间; } //获取器和设置器 } |
使用以下代码按名称对电影列表进行排序的代码 比较器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
电影 m1=新 电影(“盗梦空间”,110); 电影 m2=新 电影(“教父”,200); 电影 m3=新 电影(“阿甘正传”,130); 电影 m4=新 电影(“复仇者”,150); 清单<电影> 电影列表 = 新 数组列表<>(); 电影列表.加(m1); 电影列表.加(m2); 电影列表.加(m3); 电影列表.加(m4); 系统.出.打印(“在按名称排序之前:”); 对于 (整型 i = 0; i < 电影列表.尺寸(); i++ ) { 电影 电影 = (电影) 电影列表.得到(i); 系统.出.打印(电影); } //按movieName排序 //匿名比较器 //旧方法 馆藏.分类(电影列表, 新 比较器<电影>() { @覆写 上市 整型 相比(电影 o1, 电影 o2) { 返回 o1.得到MovieName().相比于(o2.得到MovieName()); } }); |
Anonymous classes
is syntax. For very simple operation, we need to write complex code. To solve this problem, JDK has introduced a new feature called Lambda Expressions. I will take this example after explaining lambda expression to understand, how lambda expression
will reduce this complex code.什么是Lambda表达式:
function without name
,Lambda表达式的结构
(参数列表)->{statements;}
- 参数列表或参数
- Lambda表达式可以具有零个或多个参数。
()->{System.out.println(“Hello”)}; //不带参数,会打个招呼
(int a)->{System.out.println(a)}; //一个参数,将输出a的值
(int a,int b)->{a + b}; //两个参数,将返回这两个整数的和 - 您可以选择不声明参数的类型,因为可以从上下文中推断出来。
-
(a,b)->{a + b}; //两个参数,将返回这两个数字的和
- 你不能声明一个参数’的类型,请勿为其他参数声明类型。
-
(int a,b)->{a + b}; //编译错误
- 当有一个参数时,如果推断出其类型,则不必使用括号
a->{System.out.println(a)}; //将输出数字a
- Lambda表达式可以具有零个或多个参数。
- 数组令牌(->)
- 身体
身体
可以有表达式或语句。- If there is only one
statement
in body,curly brace is not needed and return type of the anonymous function is same as of body expression - If there are more than one
statements
, then it should be incurly braces
and return type of anonymous function is same as value return from code block,虚空
if nothing is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
上市 类 线程样本 { 上市 静态的 虚空 主要(串[] args) { //旧方法 新 线(新 可运行() { @覆写 上市 虚空 跑() { 系统.出.打印(“线程已启动”); } }).开始(); //使用lambda表达式 新 线(()->系统.出.打印(“线程已启动”)).开始(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
包 组织.Arpit.爪哇2blog; 上市 类 电影 { 串 电影Name; 长 持续时间; 上市 电影(串 电影Name, 长 持续时间) { 超(); 这个.电影Name = 电影Name; 这个.持续时间 = 持续时间; } 上市 串 得到MovieName() { 返回 电影Name; } 上市 虚空 setMovieName(串 电影Name) { 这个.电影Name = 电影Name; } 上市 长 得到Duration() { 返回 持续时间; } 上市 虚空 setDuration(长 持续时间) { 这个.持续时间 = 持续时间; } @覆写 上市 串 toString() { 返回 “电影名称:” + 这个.得到MovieName() + “ ||” + “电影时长:” + 这个.得到Duration(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
包 组织.Arpit.爪哇2blog; 进口 爪哇.实用程序.数组列表; 进口 爪哇.实用程序.馆藏; 进口 爪哇.实用程序.比较器; 进口 爪哇.实用程序.清单; 上市 类 比较器LambdaMain { / ** * @作者Arpit Mandliya */ 上市 静态的 虚空 主要(串[] args) { 电影 m1=新 电影(“盗梦空间”,110); 电影 m2=新 电影(“教父”,200); 电影 m3=新 电影(“阿甘正传”,130); 电影 m4=新 电影(“复仇者”,150); 清单<电影> 电影列表 = 新 数组列表<>(); 电影列表.加(m1); 电影列表.加(m2); 电影列表.加(m3); 电影列表.加(m4); 系统.出.打印(“在按名称排序之前:”); 对于 (整型 i = 0; i < 电影列表.尺寸(); i++ ) { 电影 电影 = (电影) 电影列表.得到(i); 系统.出.打印(电影); } //按movieName排序 //匿名比较器 //旧方法 馆藏.分类(电影列表, 新 比较器<电影>() { @覆写 上市 整型 相比(电影 o1, 电影 o2) { 返回 o1.得到MovieName().相比于(o2.得到MovieName()); } }); //使用lambda表达式 馆藏.分类(电影列表, (o1, o2) -> o1.得到MovieName().相比于(o2.得到MovieName())); 系统.出.打印(“按名称排序后:”); 对于 (整型 i = 0; i < 电影列表.尺寸(); i++ ) { 电影 电影 = (电影) 电影列表.得到(i); 系统.出.打印(电影); } } } |
输出:
电影名称:盗梦空间||电影时长:110
电影名称:GodFather ||电影时长:200
电影名称:森林阿甘||电影时长:130
电影名称:复仇者联盟||电影时长:150
按名称排序后:
电影名称:复仇者联盟||电影时长:150
电影名称:森林阿甘||电影时长:130
电影名称:GodFather ||电影时长:200
电影名称:盗梦空间||电影时长:110
lambda expression
对于 using Comparator. So in spite of writing Anonymous comparator, our expression became very easy.您好World Lambda Expression Example
1 2 3 4 5 6 7 |
包 组织.Arpit.爪哇2blog; 上市 接口 您好World { 虚空 问好(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
包 组织.Arpit.爪哇2blog; 上市 类 您好WorldMain { 上市 静态的 虚空 主要(串 args[]) { // Lambda表达式 您好World 你好世界=()->系统.出.打印("您好 using Lambda Expression"); 你好世界.问好(); } } |
Lambda表达的优秀博客。