lambda表达式的特点
1 . 函数式编程
2 . 参数类型自动推导
3 . 代码量少,简洁
使用在任何有函数式接口的地方
只有一个抽象方法的接口
// 注解
@FunctionalInterface
public interface InterfaceTest {
// 函数式接口只能有一个抽象方法
// public void add( ) ;
public abstract void sub( ) ;
}
简单使用
package com. chauncy. lamda ;
@FunctionalInterface
public interface Chauncy_L {
int get ( Chauncy chauncy) ;
}
package com. chauncy. lamda ;
public class Chauncy {
}
package com. chauncy. lamda ;
public class Chauncy_Lambda {
static Integer getChuancy ( Chauncy chauncy) {
return 5 ;
}
static Integer getChuancy2 ( ) {
return 6 ;
}
public void myStart ( ) {
System . out. println ( "myStart..." ) ;
}
public static void main ( String [ ] args) {
Chauncy_L c0 = new Chauncy_L ( ) {
@Override
public int get ( Chauncy chauncy) {
return 0 ;
}
} ;
Chauncy_L c1 = ( chauncy) -> 1 ;
Chauncy_L c2 = ( chauncy) -> { return 2 ; } ;
Chauncy_L c3 = ( Chauncy chauncy) -> 3 ;
Chauncy_L c4 = ( Chauncy chauncy) -> { return 4 ; } ;
Chauncy_L c5 = Chauncy_Lambda :: getChuancy ;
Chauncy_L c6 = ( cc) -> getChuancy2 ( ) ;
System . out. println ( c0. get ( new Chauncy ( ) ) ) ;
System . out. println ( c1. get ( new Chauncy ( ) ) ) ;
System . out. println ( c2. get ( new Chauncy ( ) ) ) ;
System . out. println ( c3. get ( new Chauncy ( ) ) ) ;
System . out. println ( c4. get ( new Chauncy ( ) ) ) ;
System . out. println ( c5. get ( new Chauncy ( ) ) ) ;
System . out. println ( c6. get ( new Chauncy ( ) ) ) ;
Chauncy_Lambda lambda = new Chauncy_Lambda ( ) ;
new Thread ( lambda:: myStart ) . start ( ) ;
}
}
方法的调用方式
静态方法引用
语法
类名::静态方法
对应的lambda表达式
( args) -> 类名.静态方法( args)
package com. chauncy. lamda. ref ;
import java. util. function. * ;
public class Lambda_Static_Ref {
static Boolean isAgeOk ( int age) {
return age > 18 ;
}
static String put ( ) {
return "put.." ;
}
public static void getSize ( String str) {
System . out. println ( str. length ( ) ) ;
}
public static String toUpper ( String str) {
return str. toUpperCase ( ) ;
}
public static Integer getLength ( String str1, String str2) {
return str1. length ( ) + str2. length ( ) ;
}
public static void main ( String [ ] args) {
Predicate < Integer > pp = Lambda_Static_Ref :: isAgeOk ;
System . out. println ( pp. test ( 19 ) ) ;
Supplier < String > su = Lambda_Static_Ref :: put ;
System . out. println ( su. get ( ) ) ;
Consumer < String > consumer = Lambda_Static_Ref :: getSize ;
consumer. accept ( "chauncy" ) ;
Function < String , String > fun = Lambda_Static_Ref :: toUpper ;
Function < String , String > fun2 = Fun :: toUpper ;
System . out. println ( fun. apply ( "chauncy" ) ) ;
System . out. println ( fun2. apply ( "jin" ) ) ;
BiFunction < String , String , Integer > biFun = Lambda_Static_Ref :: getLength ;
System . out. println ( biFun. apply ( "chauncy" , "jin" ) ) ;
}
}
class Fun {
public static String toUpper ( String string) {
return string. toUpperCase ( ) ;
}
}
实例方法引用
语法
实例名::方法
对应的lambda表达式
( args) -> 实例.实例方法( args)
public class Instance_Ref_test {
public String put ( ) {
return "put..." ;
}
public void getSize ( int size) {
System . out. println ( size) ;
}
public String toUpper ( String str) {
return str. toUpperCase ( ) ;
}
public Integer toLength ( String str1, String str2) {
return str1. length ( ) + str2. length ( ) ;
}
public static void main ( String [ ] args) {
new Instance_Ref_test ( ) . put ( ) ;
Supplier < String > s1 = ( ) -> new Instance_Ref_test ( ) . put ( ) ;
Supplier < String > s2 = ( ) -> { return new Instance_Ref_test ( ) . put ( ) ; } ;
Supplier < String > s3 = new Instance_Ref_test ( ) :: put ;
System . out. println ( s1. get ( ) ) ;
System . out. println ( s2. get ( ) ) ;
System . out. println ( s3. get ( ) ) ;
new Instance_Ref_test ( ) . getSize ( 23 ) ;
Instance_Ref_test instance = new Instance_Ref_test ( ) ;
Consumer < Integer > c1 = ( a) -> instance. getSize ( a) ;
c1. accept ( 24 ) ;
Consumer < Integer > c2 = instance:: getSize ;
c2. accept ( 25 ) ;
instance. toUpper ( "chauncy" ) ;
Function < String , String > f1 = ( a) -> instance. toUpper ( a) ;
Function < String , String > f2 = instance:: toUpper ;
System . out. println ( f1. apply ( "ccc" ) ) ;
System . out. println ( f2. apply ( "dcc" ) ) ;
System . out. println ( new Instance_Ref_test ( ) . toLength ( "env" , "args" ) ) ;
BiFunction < String , String , Integer > b1 = ( a, b) -> instance. toLength ( a, b) ;
BiFunction < String , String , Integer > b2 = instance:: toLength ;
System . out. println ( b1. apply ( "pwd" , "cat" ) ) ;
System . out. println ( b2. apply ( "pwd" , "cat" ) ) ;
}
}
对象方法引用
语法
类名::实例方法
对应的lambda表达式
( ints,args) -> 类名.实例方法( args)
package com. chauncy. lamda. ref ;
import java. util. function. BiFunction ;
import java. util. function. Consumer ;
import java. util. function. Function ;
import java. util. function. Supplier ;
public class Lambda_Object_Ref {
public static void main ( String [ ] args) {
Too too1 = new Too ( ) ;
Runnable foo01 = too1:: foo ;
foo01. run ( ) ;
Supplier < Integer > sup = ( ) -> new Too ( ) . returnLength ( ) ;
Supplier < Integer > sup01 = too1:: returnLength ;
System . out. println ( sup01. get ( ) ) ;
Consumer < String > con = too1:: con ;
con. accept ( "chauncy" ) ;
Function < String , Integer > fun = too1:: fun ;
System . out. println ( fun. apply ( "chauncy" ) ) ;
BiFunction < String , String , Integer > length = too1:: getLength ;
System . out. println ( length. apply ( "chauncy" , "jin" ) ) ;
}
}
class Too {
public Integer getLength ( String firstName, String lastName) {
return firstName. length ( ) + lastName. length ( ) ;
}
public Integer fun ( String string) {
return string. length ( ) ;
}
public void con ( String name) {
System . out. println ( "Hello," + name) ;
}
public void foo ( ) {
System . out. println ( "foo..." ) ;
}
public Integer returnLength ( ) {
return 12 ;
}
}
构造方法引用
语法
类名::new
对应的lambda表达式
( args) -> new 类名( args)
public class Init_Ref_test {
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 ;
s3. get ( ) ;
Supplier < Set > s4 = HashSet :: new ;
s4. get ( ) ;
Supplier < Thread > s5 = Thread :: new ;
s5. get ( ) ;
Supplier < String > s6 = String :: new ;
s6. get ( ) ;
Consumer < String > c1 = ( a) -> new Account ( a) ;
c1. accept ( "hello" ) ;
Consumer < Integer > c2 = Account :: new ;
c2. accept ( 34 ) ;
Function < String , Account > f1 = ( str) -> new Account ( str) ;
Function < String , Account > f2 = Account :: new ;
f1. apply ( "haha" ) ;
f2. apply ( "ABc" ) ;
}
}
class Person {
public Person ( ) {
System . out. println ( "无参的构造方法... " ) ;
}
}
class Account {
public Account ( ) {
System . out. println ( "调用无参构造方法...account " ) ;
}
public Account ( int age) {
System . out. println ( "age 参数构造" + age) ;
}
public Account ( String string) {
System . out. println ( "string 参数构造 " + string) ;
}
}
lambda表达式接口
( ) -> { }
( ) -> { System.out.println( 1 ) ; }
( ) -> System.out.println( 1 )
( ) -> ( return 100 ; }
( ) -> 100
( ) -> null
( int x) -> { return x1; }
( int x) -> x+1
x-> x+1
常用函数式接口
Runnable 无输入 无输出
Runnable
void run( )
Runnable foo = too1::foo; // foo 为无入参,无出参的函数
foo.run( ) ;
Predicate 测试
Predicate< T> 一个输入 Boolean输出
boolean test( T t)
Predicate< String> ok = Lamda_Static_Ref::isOK;
System.out.println( ok.test( "chauncy" )) ;
两个输入 一个Boolean输出
BiPredicate< T, U>
boolean test( T t, U u)
Consumer 消费 没有输出
Consumer< T>
void accept( T t)
Consumer< String> consumer1 = ( a) -> System.out.println( a ) ;
consumer.accept( "name" ) ;
BiConsumer< T, U>
void accept( T t, U u)
Function 输入 -> 输出
Function< T, R>
R apply( T t)
BiFunction< T, U, R>
R apply( T t, U u)
BiFunction< String,String,Integer> biFunction = ( a, b) -> a.length( ) +b.length( ) ;
System.out.println( biFunction.apply( "chauncy" , "好帅" )) ;
Callable 0输入 -> 1输出
Callable< V>
V call( )
Supplier 0输入 -> 1输出
Supplier< T>
T get( )
Supplier< String> supplier = ( ) -> "23" ;
System.out.println( supplier.get( )) ;