lambda表达式定义:
lambda表达式特征:
lambda表达式应用场景:
任何有函数式接口的地方,只有一个方法(Object类中的方法除外)的接口是函数式接口,用@FunctionalInterface来检验是不是函数式接口
内置函数式接口:
以Function为例:
Function<String,Integer> f1 = (str)->{return str.length();};
System.out.println(f1.apply("abcdefg"));
该接口有一个输入一个输出,所以String为我们设定的输入类型,Integer为我们设定的输出类型。(str)代表输入参数,{ }中的内容为重写的方法体,返回的是length形的int,该方法必须利用apply调用
再如只有一个输出的函数式接口:
Consumer<String> c11 = (str) -> System.out.println(str);
c11.accept("beijing");
当重写的方法体中只有一条语句,则可以不用写大括号{},当如过有不止一条语句,就需要用{},且每条语句需要用分号隔开。用accept调用,并传入要输出的内容
两个输入,一个输出:
BiFunction<String,String,Integer> bf = (a,b)->a.length()+b.length();
System.out.println(bf.apply("连老师", "好帅"));
也可以自己定义函数式接口
main{
Runnable runnable1 = ()->{int i = get(); System.out.println(i);}; //runnable中本来是没有返回值的,但是通过这种方法可以拥有返回值
Runnable runnable2 = ()->exec();
// Runnable runnable3 = ()->100;//不可取,runnalbe中没有返回值
// Runnable runnable4 = ()->"";//不可取,runnalbe中没有返回值
// runnable1.run();
}
static int get(){
return 1;
}
static String find(){
return "find";
}
static void exec(){
find();
}
一种新型的调用方式:方法引用
对于静态方法,不是同一个类中也可以用 类名::方法名 ,要在外面先把该方法定义好
public class Test2 {
static String put(){
System.out.println("put.....");
return "put";
}
public static void getSize(int size){
System.out.println(size);
}
public static String toUpperCase(String str){
return str.toUpperCase();
}
public static Integer getLength(String str,String str2){
return str.length()+str2.length();
}
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;
Consumer<Integer> c2 = (size)->Test2.getSize(size);
c1.accept(123);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String> f2 = (str)->Test2.toUpperCase(str);
Function<String,String> f3 = Test2::toUpperCase;
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"));
BiFunction<String,String,Integer> bf = (a,b)->a.length()+b.length();
BiFunction<String,String,Integer> bf2 = Test2::getLength;
System.out.println(bf2.apply("abc", "def"));
System.out.println(bf.apply("abc", "def"));
}
}
class Fun{
public static String hehe(){
return "hehe";
}
public static String toUpperCase(String str){
return str.toUpperCase();
}
}
实例方法 的引用: new 类名::方法名
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 test = new Test3();
Consumer<Integer> c1 = (size)->new Test3().getSize(size);
Consumer<Integer> c2 = new Test3()::getSize;
Consumer<Integer> c3 = test::getSize;
//进行方法调用
c1.accept(123);
c2.accept(123);
c3.accept(123);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String> f2 = (str)->test.toUpperCase(str);
Function<String,String> f3 = new Test3()::toUpperCase;
Function<String,String> f4 = test::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 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().show();
// 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<Exec,String,Integer> bf1 = (e,s)->new Exec().test(s);
bf1.apply(new Exec(),"abc");
BiFunction<Exec,String,Integer> bf2 = Exec::test;
bf2.apply(new Exec(),"def");
}
}
class Exec{
public int test(String name){
return 1;
}
}
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);
}
}
构造方法的引用
public class Test5 {
public static void main(String[] args) {
Supplier<Person> s1 = ()->new Person();
s1.get();
Supplier<Person> s2 = Person::new;
s2.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;
c1.accept(123);
c2.accept(456);
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("str 参数构造" +str);
}
}
class Person{
public Person(){
System.out.println("调用无参的构造方法");
}
}