共计 7503 个字符,预计需要花费 19 分钟才能阅读完成。
一、Lambda 表达式介绍
1、Lambda 表达式是 java8 中最重要的新性能之一。应用 Lambda 表达式能够代替只有一个形象函数接口的实现,辞别匿名外部类,代码看起来更简洁易懂。Lambda 表达式同时还提醒了对汇合框架的迭代、遍历、过滤数据的操作
2、特点
(1)函数式编程
(2) 参数类型主动推断
(3) 代码量少,简洁3、利用场景
(1)任何有函数式接口的中央
二、Lambda 表达式的应用
示例
Student
package com.msbline.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 +'}';
}
}
接口:StudentFilter
package com.msbline.lambda.v1;
import com.msbline.lambda.Student;
public interface StudentFilter {boolean compare(Student student);
}
实现类 1:
public class AgeFilter implements StudentFilter {
@Override
public boolean compare(Student student) {return student.getAge()>3;
}
}
实现类 2:
package com.msbline.lambda.v1;
import com.msbline.lambda.Student;
public class ScoreFilter implements StudentFilter{
@Override
public boolean compare(Student student) {return student.getScore()>1;
}
}
测试:
原始操作
package com.msbline.lambda.v1;
import com.msbline.lambda.Student;
import java.util.ArrayList;
public class Test {public static void main(String[] args) {ArrayList<Student> list = new ArrayList<>();
list.add(new Student("zhangsan",1,1));
list.add(new Student("lisi",8,2));
list.add(new Student("wangwu",3,3));
list.add(new Student("jack",9,4));
list.add(new Student("rose",5,5));
getByFilter(list,new AgeFilter());
System.out.println("-----------------");
getByFilter(list,new ScoreFilter());
}
public static void getByFilter(ArrayList<Student> students,StudentFilter filter){ArrayList<Student> list = new ArrayList();
for (Student stu:students){if(filter.compare(stu)){list.add(stu);
}
}
print(list);
}
public static void print(ArrayList<Student> students){for (Student stu:students){System.out.println(stu);
}
}
}
lambda 操作, 都能够不必接口和实现类
package com.msbline.lambda.v1;
import com.msbline.lambda.Student;
import java.util.ArrayList;
public class Test3 {public static void main(String[] args) {ArrayList<Student> list = new ArrayList<>();
list.add(new Student("zhangsan",1,1));
list.add(new Student("lisi",8,2));
list.add(new Student("wangwu",3,3));
list.add(new Student("jack",9,4));
list.add(new Student("rose",5,5));
getByFilter(list,(e)->e.getAge()>3);
System.out.println("------------------");
getByFilter(list,(e)->e.getScore()>1);
System.out.println("------------------");
getByFilter(list,(e)->e.getName().length()>4);
}
public static void getByFilter(ArrayList<Student> students,StudentFilter filter){ArrayList<Student> list = new ArrayList();
for (Student stu:students){if(filter.compare(stu)){list.add(stu);
}
}
print(list);
}
public static void print(ArrayList<Student> students){for (Student stu:students){System.out.println(stu);
}
}
}
三、函数式接口
只有一个形象办法(Object 类中的办法除外)的接口是函数式接口
四、Lambda 表达式的原理
临时搞不明确,前期补充
五、办法的援用
办法援用是用来间接拜访类或者实例的曾经存在的办法或者构造方法,办法援用提供了一种援用而不执行办法的形式,如果形象办法的实现恰好能够应用调用另外一个办法来实现,就有可能能够应用办法援用
1、静态方法援用
如果函数式接口的实现恰好能够通过调用一个静态方法来实现,那么就能够应用静态方法援用
package com.msbline.lambda.functionRef;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class Test2 {public static void main(String[] args) {// System.out.println(put());
Supplier<String> s1 = ()->Test2.put();
System.out.println(s1.get());
Supplier<String> s2 = Test2::put;
System.out.println(s2.get());
Supplier<String> s3 = Fun::hehe;
System.out.println(s3.get());
Consumer<Integer> c1 = Test2::getSize;
c1.accept(1);
Consumer<Integer> c2 = (size)->Test2.getSize(size);
c2.accept(123);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String> f2 = (str)->Test2.toUpper(str);
Function<String,String> f3 = Test2::toUpper;
Function<String,String> f4 = Fun::toUpperCase;
System.out.println(f1.apply("abc"));
System.out.println(f2.apply("abc"));
System.out.println(f3.apply("abc"));
System.out.println(f4.apply("abc"));
}
public static void getSize(int size){System.out.println(size);
}
public static String toUpper(String str){return str.toUpperCase();
}
static String put(){System.out.println("put......");
return "put";
}
}
class Fun{public static String hehe(){return "hehe";}
public static String toUpperCase(String str){return str.toUpperCase();
}
}
2、实例办法援用
如果函数式接口的实现恰好能够通过调用一个实例的实例办法来实现,那么久能够应用实例办法援用
package com.msbline.lambda.functionRef;
import com.msbline.threadPkg.Test;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class Test3 {public String put(){return "put......";}
public void getSize(int size){System.out.println("size:"+size);
}
public String toUppercase(String str){return str.toUpperCase();
}
public static void main(String[] args) {System.out.println(new Test3().put());
Supplier<String> s1 = ()->new Test3().put();
Supplier<String> s2 = ()->{return new Test3().put();};
Supplier<String> s3 = new Test3()::put;
System.out.println(s1.get());
System.out.println(s2.get());
System.out.println(s3.get());
Test3 test3 = new Test3();
Consumer<Integer> c1 = (size)->new Test3().getSize(size);
Consumer<Integer> c2 = new Test3()::getSize;
Consumer<Integer> c3 = test3::getSize;
c1.accept(123);
c2.accept(123);
c3.accept(123);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String> f2 = (str)->test3.toUppercase(str);
Function<String,String> f3 = new Test3()::toUppercase;
Function<String,String> f4 = test3::toUppercase;
System.out.println(f1.apply("abc"));
System.out.println(f2.apply("abc"));
System.out.println(f3.apply("abc"));
System.out.println(f4.apply("abc"));
}
}
3、对象办法援用
形象办法的第一个参数类型刚好是实例办法的类型,形象办法残余的参数恰好可当作实例办法的参数,如果函数式接口的实现能由下面说的实例办法调用来实现的话,那么就能够应用对象办法援用
package com.msbline.lambda.functionRef;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
public class Test4 {public static void main(String[] args) {Consumer<Too> c1 = (Too too)->new Too().foo();
c1.accept(new Too());
Consumer<Too> c2 = (Too too) ->new Too2().foo();
c2.accept(new Too());
Consumer<Too> c3 = Too::foo;
c3.accept(new Too());
BiConsumer<Too2,String> bc = (too2,str)->new Too2().show(str);
BiConsumer<Too2,String> bc2 = Too2::show;
bc.accept(new Too2(),"abc");
bc2.accept(new Too2(),"def");
BiFunction<Too2,String,Integer> bf1 = (e,s)->new Exec().test(s);
bf1.apply(new Too2(),"abc");
BiFunction<Exec,String,Integer> bf2 = Exec::test;
bf2.apply(new Exec(),"def");
}
}
class Exec{public int test(String name){return 2;}
}
class Too{public Integer fun(String s){return 1;}
public void foo(){System.out.println("foo");
}
}
class Too2{public Integer fun(String s){return 1;}
public void foo(){System.out.println("foo---too2");
}
public void show(String str){System.out.println("show---too2"+str);
}
}
4、构造方法援用
如果函数式接口的实现恰好能够通过调用一个类的构造方法来实现,那么就能够应用构造方法援用
package com.msbline.lambda.functionRef;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class Test5 {public static void main(String[] args) {Supplier<Person> s1 = ()->new Person();
s1.get();
Supplier<Person> s2 = Person::new;
s1.get();
Supplier<List> s3 = ArrayList::new;
Supplier<Set> s4 = HashSet::new;
Supplier<Thread> s5 = Thread::new;
Supplier<String> s6 = String::new;
Consumer<Integer> c1 = (age)->new Account(age);
Consumer<Integer> c2 = Account::new;
Consumer<Integer> c3 = Integer::new;
c1.accept(2);
c2.accept(2);
c3.accept(5);
Function<String,Account> f1 = (str)->new Account(str);
Function<String,Account> f2 = Account::new;
f1.apply("abc");
f2.apply("def");
}
}
class Account{public Account() {System.out.println("调用无参构造方法");
}
public Account(int age){System.out.println("age 参数结构"+age);
}
public Account(String str){System.out.println("String 参数结构"+str);
}
}
class Person{public Person() {System.out.println("调用无参结构");
}
}
正文完