Lamda表达式
1. 为什么要使用lambda表达式
避免匿名内部类定义过多 可以让你的代码看起来很简洁 去掉一堆没用意义的代码,只留下核心的逻辑
2. Functional Interface(函数式接口)
定义:任何接口,如果只包含一个抽象方法,那么它就是一个函数式接口。
public interface Runnable {
public abstract void run ( ) ;
}
对于函数式接口,我们可以通过lambada表达式来创建该接口的对象
推导lambda表达式:
package lamada;
public class TestLambda {
static class Like2 implements ILike {
@Override
public void lambda ( ) {
System. out. println ( "3.静态内部类 : lambda实现静态内部类方法" ) ;
}
}
public static void main ( String[ ] args) {
ILike like = new Like ( ) ;
like. lambda ( ) ;
like = new Like2 ( ) ;
like. lambda ( ) ;
class Like3 implements ILike {
@Override
public void lambda ( ) {
System. out. println ( "4.局部内部类 : lambda实现局部内部类方法" ) ;
}
}
like = new Like3 ( ) ;
like. lambda ( ) ;
like = new ILike ( ) {
@Override
public void lambda ( ) {
System. out. println ( "5.匿名内部类 : lambda实现匿名内部类方法" ) ;
}
} ;
like. lambda ( ) ;
like = ( ) - > {
System. out. println ( "6.lambda简化 : lambda实现简化方法" ) ;
} ;
like. lambda ( ) ;
}
}
interface ILike {
void lambda ( ) ;
}
class Like implements ILike {
@Override
public void lambda ( ) {
System. out. println ( "2.实现类 : lambda实现类方法" ) ;
}
}
1. 实现类
package lamada;
public class TestLambda2 {
public static void main ( String[ ] args) {
ITest test = new Test ( ) ;
test. test ( 666 ) ;
}
}
interface ITest {
void test ( int i) ;
}
class Test implements ITest {
@Override
public void test ( int i) {
System. out. println ( "测试lambda,参数为" + i) ;
}
}
2.静态内部类
package lamada;
public class TestLambda2 {
static class Test implements ITest {
@Override
public void test ( int i) {
System. out. println ( "测试lambda,参数为" + i) ;
}
}
public static void main ( String[ ] args) {
ITest test = new Test ( ) ;
test. test ( 666 ) ;
}
}
interface ITest {
void test ( int i) ;
}
3.局部内部类
package lamada;
public class TestLambda2 {
public static void main ( String[ ] args) {
class Test implements ITest {
@Override
public void test ( int i) {
System. out. println ( "测试lambda,参数为" + i) ;
}
}
ITest test = new Test ( ) ;
test. test ( 666 ) ;
}
}
interface ITest {
void test ( int i) ;
}
4.匿名内部类
package lamada;
public class TestLambda2 {
public static void main ( String[ ] args) {
ITest test = new ITest ( ) {
@Override
public void test ( int i) {
System. out. println ( "测试lambda,参数为" + i) ;
}
} ;
test. test ( 666 ) ;
}
}
interface ITest {
void test ( int i) ;
}
5.简化为lambda
package lamada;
public class TestLambda2 {
public static void main ( String[ ] args) {
ITest test = ( int i) - > {
System. out. println ( "测试lambda,参数为" + i) ;
} ;
test = ( i) - > {
System. out. println ( "测试lambda,参数为" + i) ;
} ;
test = i - > {
System. out. println ( "测试lambda,参数为" + i) ;
} ;
test = i - > System. out. println ( "测试lambda,参数为" + i) ;
test. test ( 666 ) ;
}
}
interface ITest {
void test ( int i) ;
}
总结:
lambda表达式只能有一行代码的情况下才能简化为一行,如果有多行,那就用代码块包裹。 前提是接口为函数式接口 多个参数也可以去掉参数类型,要去掉就都去掉
package lamada;
public class TestLambda2 {
public static void main ( String[ ] args) {
ITest test = null;
test = ( a, b, c) - > {
System. out. println ( "测试lambda,参数1为" + a) ;
System. out. println ( "测试lambda,参数2为" + b) ;
System. out. println ( "测试lambda,参数3为" + c) ;
} ;
test. test ( 666 , 8888 , 999 ) ;
}
}
interface ITest {
void test ( int a, int b, int c) ;
}