Lambda表达式介绍
Lambda表达式是Java8中最重要的新功能之一.使用Lambda表达式可以替代只有一个抽象函数的接口实现,告别匿名内部类,代码看起来简洁易懂.Lambda表达式同时还提升了对集合、框架的迭代、遍历、过滤数据的操作.
为什么要用lambda表达式
首先查看案例
public class Student {
private String name;
private int age;
private int score;
public Student() {
}
public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}
}
public class Test {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("zhangsan",14,67));
list.add(new Student("lisi",13,89));
list.add(new Student("wangwu",15,97));
list.add(new Student("maliu",12,63));
list.add(new Student("zhaoqi",17,75));
//查找年龄大于14的学生
findByAge(list);
System.out.println("---------------------");
//查找分数大于75的学生
findByScore(list);
}
public static void findByAge(ArrayList<Student> students){
ArrayList<Student> list =new ArrayList<>();
for(Student stu:students){
if(stu.getAge()>14){
list.add(stu);
}
}
for(Student student :list){
System.out.println(student);
}
}
public static void findByScore(ArrayList<Student> students){
ArrayList<Student> list =new ArrayList<>();
for(Student stu:students){
if(stu.getScore()>75){
list.add(stu);
}
}
for(Student student :list){
System.out.println(student);
}
}
}
对上面的代码进行优化
public interface StudentFilter {
boolean compare(Student student);
}
public class ScoreFilter implements StudentFilter {
@Override
public boolean compare(Student student) {
return student.getScore()>75;
}
}
public class AgeFilter implements StudentFilter {
@Override
public boolean compare(Student student) {
return student.getAge()>14;
}
}
public class Test {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("zhangsan",14,67));
list.add(new Student("lisi",13,89));
list.add(new Student("wangwu",15,97));
list.add(new Student("maliu",12,63));
list.add(new Student("zhaoqi",17,75));
getByFilter(list,new AgeFilter());
getByFilter(list,new ScoreFilter());
}
public static void getByFilter(ArrayList<Student> students,StudentFilter filter){
ArrayList<Student> list = new ArrayList<>();
for(Student student:students){
if(filter.compare(student)){
list.add(student);
}
}
printStudent(list);
}
public static void printStudent(ArrayList<Student> students){
for(Student student:students){
System.out.println(student);
}
}
}
当使用Lambda表达式时
public class Test {
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<Student>();
list.add(new Student("zhangsan",14,67));
list.add(new Student("lisi",13,89));
list.add(new Student("wangwu",15,97));
list.add(new Student("maliu",12,63));
list.add(new Student("zhaoqi",17,75));
getByFilter(list,(e)->e.getAge()>14 );
getByFilter(list, (e)->e.getScore()>75);
System.out.println("-------------------");
getByFilter(list, (e)->e.getName().length()>5);
}
public static void getByFilter(ArrayList<Student> students, StudentFilter filter){
ArrayList<Student> list = new ArrayList<>();
for(Student student:students){
if(filter.compare(student)){
list.add(student);
}
}
printStudent(list);
}
public static void printStudent(ArrayList<Student> students){
for(Student student:students){
System.out.println(student);
}
}
}
Lambda表达式格式
任何有函数式接口的地方都可使用Lambda表达式
(参数类型 参数名称) -> {
方法体;
return 返回值;
}
格式说明
(参数类型 参数名称):参数列表部分
{...}:方法体,即要执行的代码部分
->:箭头,无实际含义,起到连接参数列表和方法体的作用
Lambda表达式的省略规则
小括号中的参数类型可以省略。
如果小括号中只有一个参数,那么可以省略小括号。
如果大括号中只有一条语句,那么可以同时省略大括号、return关键字及语句分号。