Lambda
什么是lambda表达式。(官方解释) Lambda 表达式(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。 匿名函数:
匿名函数的基本形式为(function(){…})(); 。前面的括号包含函数体,后面的括号就是给匿名函数传递参数并立即执行之。匿名函数的作用是用于闭包和避免全局变量的污染以及函数名的冲突。
闭包:
函数和对其周围状态(lexical environment,词法环境)的引用捆绑在一起构成闭包(closure)。
主要作用
Lambda表达式不仅让代码变的简单、而且可读、最重要的是代码量也随之减少很多
Lambda表达式的语法
基本语法: (parameters) -> expression 或 (parameters) ->{ statements; }
举例演示
1.官网给的例子
没有使用Lambda的老方法:
button
.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent actionEvent
){
System
.out
.println("Action detected");
}
});
使用Lambda:
button
.addActionListener( actionEvent
-> {
System
.out
.println("Action detected");
});
让我们来看一个更明显的例子。 不采用Lambda的老方法:
Runnable runnable1
=new Runnable(){
@Override
public void run(){
System
.out
.println("Running without Lambda");
}
};
使用Lambda:
Runnable runnable2
=()->System
.out
.println("Running from Lambda");
2.自己的举例讲解
首页先来几个简单的
() -> 5
x
-> 2 * x
(x
, y
) -> x – y
(int x
, int y
) -> x
+ y
(String s
) -> System
.out
.print(s
)
项目实例一:获取 List 中的数据可以使用(x) -> x
public static void main(String
[] args
) {
List
<Integer
> numbers
= Arrays
.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
IntSummaryStatistics stats
= numbers
.stream().mapToInt((x
) -> x
).summaryStatistics();
System
.out
.println("List中最大的数字 : " + stats
.getMax());
System
.out
.println("List中最小的数字 : " + stats
.getMin());
System
.out
.println("所有数字的总和 : " + stats
.getSum());
System
.out
.println("所有数字的平均值 : " + stats
.getAverage());
}
项目实例二:获取某一字段的数据 (x) -> x.getAge()
public static void main(String
[] args
) {
List
<Sswt
> myPlayers
= Arrays
.asList(
new Sswt(52, "张三", 54, 80, "上海"),
new Sswt(31, "李四", 54, 80, "北京"),
new Sswt(21, "王二", 54, 80, "河南"),
new Sswt(20, "麻子", 54, 80, "广州"));
int s
= myPlayers
.stream().mapToInt(p
-> p
.getWeight()).sum();
System
.out
.println(s
);
long ss
= myPlayers
.stream().mapToInt(p
-> p
.getWeight()).count();
System
.out
.println(ss
);
IntSummaryStatistics playerCalc
= myPlayers
.stream().mapToInt((x
) -> x
.getAge()).summaryStatistics();
System
.out
.println("List中最大的数字 : " + playerCalc
.getMax());
System
.out
.println("List中最小的数字 : " + playerCalc
.getMin());
System
.out
.println("所有数字的总和 : " + playerCalc
.getSum());
System
.out
.println("所有数字的平均值 : " + playerCalc
.getAverage());
List
<Integer
> numbers
= Arrays
.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
IntSummaryStatistics stats
= numbers
.stream().mapToInt((x
) -> x
).summaryStatistics();
System
.out
.println("List中最大的数字 : " + stats
.getMax());
System
.out
.println("List中最小的数字 : " + stats
.getMin());
System
.out
.println("所有数字的总和 : " + stats
.getSum());
System
.out
.println("所有数字的平均值 : " + stats
.getAverage());
}
Sswt
public class Sswt {
public Sswt(int age
, String name
, int height
, int weight
, String home
) {
this.age
= age
;
this.name
= name
;
this.height
= height
;
this.weight
= weight
;
this.home
= home
;
}
private int age
;
private String name
;
private int height
;
private int weight
;
private String home
;
public int
getAge() {return age
; }
public void setAge(int age
) {this.age
= age
; }
public String
getName() { return name
; }
public void setName(String name
) { this.name
= name
; }
public int
getHeight() { return height
; }
public void setHeight(int height
) { this.height
= height
; }
public int
getWeight() {return weight
; }
public void setWeight(int weight
) { this.weight
= weight
; }
public String
getHome() { return home
; }
public void setHome(String home
) { this.home
= home
; }
}
项目实例三:for循环
myPlayers
.forEach( p
-> System
.out
.println(p
));
for(String player
: players
) {
System
.out
.println(player
+ ";");
}
其他项目实例
网上说的有排序,过滤等我这里就不写了。。
总结
lambda表达式的使用还是要看自己的业务场景。希望这边文章能帮到大家!!最后提醒大家这是jdk1.8的环境哦。