这篇文章给大家分享的是有关java中怎样使用Lambda表达式的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
首先Lambda表达式的最简单应用如下
String lam= "初次相识Lambda";
new Thread(() -> System.out.println(lam)).start();
String tradition="传统方法";
new Thread(new Runnable() {
@Override
public void run() {
System.out.println(tradition);
}
}).start();
输出结果
很简洁有没有?省略了好多代码是不是,在这里 你可以发现”,Lambda表达式和在Thread创建一个匿名类的作用是一样。我们可以这样认为Lambda表达式本身代表了一个匿名类。
这就是Lambda最大的作用,当然Lambda表达式只能创建接口interface对象。 创建类是不行的,抽象类也是不行的 ,只要是类都是不行的。
首先,我定义了一个自定义的接口,可以用来测试
@FunctionalInterface
public interface Lam {
//Lambda表达式调用的方法
void bda();
//接口的默认方法
default void test(){
System.out.println("我是默认的方法");
};
//接口的静态方法
static void test1(){
System.out.println("我是静态方法");
}
}
1.Lambda表达式只能是接口 interface的创建(PS从上面的例子可以看出来,Runnable是接口,可以查看源代码),并且这个接口只能包含一个方法(除了default方法和static方法)。在接口中创建default方法和static方法都必须要实现方法体如下图
2.如果你用Lambda表达式来创建类 class,则会出现以下错误 ”Target type of a lambda conversion must be an interface“如果你怕自己的定义的接口不符合Lambda表达式的规范 ,你可以在接口interfaca 上面添加注解@FunctionalInterface
3.Lambda表达式的规范表示格式 (parameters) ->{ statements; }。在某些时刻,你还可以简化这个格式
//接口定义的方法无参数时候,并且你想要执行操作也只有一句代码的时候,Lambda会自动返回一句代码,并且可以不用加{}
Lam lam1=()->System.out.println("无参数");
你可以发现后面的大括号{ }没了,这是因为后面代码如果只有一句的话,是可以省略{ } 的
我们把Lam接口定义的调用方法参数修改一下,多出了一个String类型的形参s
//Lambda表达式调用的方法
void bda(String s);
这时候 我们如果使用Lambda表达式,则可以这样
//接口定义的方法有参数时候,并且你想要执行的操作也只有一句代码的时候
Lam lam1=e->System.out.println(e);//这一句还有简化版本 Lam lam1=System.out::println;
lam1.bda("4556");
你又会发现,前面的()中括号也没了,这是因为当参数只有一个的时候,是可以省略()的。
当然也有你要执行很多代码的时候,那这时候可以这样
//接口定义的方法有参数时候,并且你想要执行的操作有很多句代码的时候
Lam lam1 = (String e) -> {
String a = e + "add";
System.out.println(a);
};
lam1.bda("test+");
输出结果如下
当然你还会问Lambda表达式能不能返回东西呢?这是肯定能的,首先我们再把上面的Lam接口方法修改一下
//Lambda表达式调用的方法
String bda(String s);
让bda方法返回一个String值,这次如果我们用Lambda的话
//接口定义的方法有返回值的时候
Lam lam1=s ->{System.out.println(s);return "我是返回的数据";};
lam1.bda("test1");
System.out.println(lam1.bda("test2"));
运行的结果:
总结 Lambda表达式 就是用来创建一个匿名的接口对象,即 它本身就是一个接口的匿名实例。只不过这个接口 有一些条件限制。
规则
1.只能是接口
否则报:Target type of a lambda conversion must be an interface
2.只能有一个public方法
否则报:Multiple non-overriding abstract methods found AInterface
或AInterface is not a functional interface
括号形式
testA((int i, int j) -> {});参数要与接口一致
public class Go {
public static void main(String a[]) {
//正确示范
testA((int i, int j) -> {});
//错误示范:Multiple non-overriding abstract methods found xxx;只能有一个public方法
testB((int i, int j) -> {});
//错误示范:Target type of a lambda conversion must be an interface;只能是接口
testC((int i, int j) -> {});
}
public static void testA(AInterface t) { }
public static void testC(CInterface t) {}
public static void testB(BInterface t) {}
interface AInterface {
void xxx(int i, int j);
}
interface BInterface {
void xxx(int i, int j);
void YYY(int i, int j);
}
abstract class CInterface {
abstract void xxx(int i, int j);
}
}
双冒号后面必须是静态方法
否则报错:Non-static method cannot be referenced from a static context
双冒号后面的方法与接口方法参数一样
方法与接口的权限可以不一样
返回类型:如果接口里面方法是void,双冒号后的方法可以任意返回类型,否则要一致
public class Go {
public static void main(String a[]) {
//之前的写法
testA(new AInterface() {
@Override
public void xxx(int i, int j) {
}
});
//正确,相对与接口里面xxx方这是改成静态和换了个名字
testA(Go::mydog);
//正确,加了返回类型和public换成private,也是ok
testA(Go::mydog2);
//错误:Non-static method cannot be referenced from a static context
testA(Go::mydog3);
//这样写也是ok的。
AInterface aInterface = Go::mydog;
testA(aInterface);
}
public static void testA(AInterface t) {
t.xxx(1, 2);
}
interface AInterface {
void xxx(int i, int j);
}
public static boolean mydog(int i, int j) {
System.out.println("mydog" + i + " & " + j);
return false;
}
private static void mydog2(int i, int j) {
System.out.println("mydo2" + i + " & " + j);
}
public void mydog3(int i, int j) {
System.out.println("mydog3" + i + " & " + j);
}
}
感谢各位的阅读!关于“java中怎样使用Lambda表达式”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。