本章概述:本章主要讲了Lambda表达式在Java中的使用,Lambda表达式的介绍,Lambda表达式的使用,函数式接口,Lambda表达式的原理。
本章概述:
一、funtionref
1)Demo1
2)Demo2
3)Demo3
4)Demo4
二、lambda
1)LambdaInterface
2)LambdaTest
3)Student
4)StudentDao
5)Teacher
6)TeacherDao
三、why1
1)Student
2)Test
四、why2
1)AgrFilter
2)ScoreFilter
3)StudentFilter
4)Test
五、why3
1)Test
六、why4
1)Test
七、LambdaDemo
八、LambdaInterface
-----------------------------------------------------------------------------------------------------------------------------
一、funtionref
1)Demo1.java
public class Demo1 {
static String put(){
System.out.println("put... ...");
return "put";
}
public static void getNum(int num){
System.out.println("num:"+num);
}
public static String toUpperCase(String str){
return str.toUpperCase();
}
public static Integer getStrLength(String str1,String str2){
return str1.length()+str2.length();
}
public static void main(String[] args) {
System.out.println(put());
Supplier<String> s1 =()->Demo1.put();
Supplier<String> s2 =()->{return Demo1.put();};
Supplier<String> s3 =Demo1::put;
System.out.println(s1.get());
System.out.println(s2.get());
System.out.println(s3.get());
Consumer<Integer> consumer1 = (num)->Demo1.getNum(num);
Consumer<Integer> consumer2 = Demo1::getNum;
consumer1.accept(200);
consumer2.accept(300);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String> f2 = (str)->{return Demo1.toUpperCase(str);};
Function<String,String> f3 = (str)->Demo1.toUpperCase(str);
Function<String,String> f4 = Demo1::toUpperCase;
Function<String,String> f5 = Fun::toUpperCase;
System.out.println(f1.apply("abcdefg"));
System.out.println(f2.apply("hijklmn"));
System.out.println(f3.apply("opqrst"));
System.out.println(f4.apply("uvw"));
System.out.println(f5.apply("xyz"));
BiFunction<String,String,Integer> b1 = Demo1::getStrLength;
BiFunction<String,String,Integer> b2 = (a,b)->Demo1.getStrLength(a,b);
BiFunction<String,String,Integer> b3 = (a,b)->{return Demo1.getStrLength(a,b);};
System.out.println(b1.apply("45679","asffg"));
System.out.println(b2.apply("45679","asffg"));
System.out.println(b3.apply("45679","asffg"));
}
}
class Fun{
public static String toUpperCase(String str){
return str.toUpperCase();
}
}
2)Demo2.java
public class Demo2 {
public void getNum(int num){
System.out.println("num:"+num);
}
public String toUpperCase(String str){
return str.toUpperCase();
}
public Integer getStrLength(String str1,String str2){
return str1.length()+str2.length();
}
public static void main(String[] args) {
new Demo2().getNum(10);
Consumer<Integer> c1 =(num)->new Demo2().getNum(num);
Consumer<Integer> c2 =(num)->{new Demo2().getNum(num);};
Consumer<Integer> c3 = new Demo2()::getNum;
c1.accept(123);
c2.accept(123);
c3.accept(123);
Function<String,String> f1 = (str)->str.toUpperCase();
Function<String,String> f2 = (str)->new Demo2().toUpperCase(str);
Function<String,String> f3 = (str)->{return new Demo2().toUpperCase(str);};
Function<String,String> f4 = new Demo2()::toUpperCase;
System.out.println(f1.apply("sdfg"));
System.out.println(f2.apply("sdfg"));
System.out.println(f3.apply("sdfg"));
System.out.println(f4.apply("sdfg"));
BiFunction<String,String,Integer> bf1 = (str1,str2)->new Demo2().getStrLength(str1,str2);
BiFunction<String,String,Integer> bf2 = (str1,str2)->{return new Demo2().getStrLength(str1,str2);};
BiFunction<String,String,Integer> bf3 = new Demo2()::getStrLength;
System.out.println(bf1.apply("454665", "sddvv"));
System.out.println(bf2.apply("