1.8 新特性之 Lambda Expressions
Lambda expressions are allowed only at source level 1.8 or above
The target type of this expression must be a functional interface
https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html
由来:
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data.
The previous section, Anonymous Classes, shows you how to implement a base class without giving it a name. Although this is often more concise than a named class, for classes with only one method, even an anonymous class seems a bit excessive and cumbersome. Lambda expressions let you express instances of single-method classes more compactly.
应用对象(functional interface)
A functional interface is any interface that contains only one abstract method.
语法:
- A comma-separated list of formal parameters enclosed in parentheses.
- The arrow token,
-> - A body, which consists of a single expression or a statement block.
举例(参数类型可以省略,单参数可省略() parentheses,单语句可省{}brace):
import org.junit.Test;
public class SomeoneTest {
@Test
public void test1() {
Someone a = new Someone() {
@Override
public String doSomething(String arg) {
return "::" + arg;
}
};
System.out.println(a.doSomething("a"));
}
@Test
public void test2() {
Someone b = (String arg) -> {
return "::" + arg;
};
System.out.println(b.doSomething("b"));
}
@Test
public void test3() {
Someone c = arg -> "::" + arg;
System.out.println(c.doSomething("c"));
}
}
interface Someone {
String doSomething(String arg);
}
多参数必须加()
import org.junit.Test;
public class DogTest {
@Test
public void test1() {
Dog a = (s1, s2) -> {
System.out.println(s1 + s2);
};
a.bark("wang", "wang");
}
@Test
public void test2() {
Dog b = (s1, s2) -> System.out.println(s1 + s2);
b.bark("wang", "wang");
}
}
interface Dog {
void bark(String s1, String s2);
}
无参数必须是 () ->
import org.junit.Test;
public class CatTest {
@Test
public void test1() {
Cat a = () -> System.out.println("miao~");
a.meow();
}
}
interface Cat {
void meow();
}
更多高级用法见 package java.util.function;
1.8 新特性之 Lambda Expressions的更多相关文章
- jdk8新特性之lambda expressions
本文分两部分: 语法简单说明 lambda的使用 注:这两部分内容均以类+注释的方式进行说明,并且内容均来自官方教程(https://docs.oracle.com/javase/tutorial/j ...
- Java8新特性系列-Lambda
转载自:Java8新特性系列-Lambda – 微爱博客 Lambda Expressions in Java 8 Lambda 表达式是 Java 8 最流行的特性.它们将函数式编程概念引入 Jav ...
- java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合
java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合 比如,我有一张表: entity Category.java service CategoryServic ...
- JDK8新特性之一Lambda
JDK8的新特性之一Lambda能将函数作为方法里面的参数使用. /** * JDK8新特性Lambda */ public class Test { public static void main( ...
- Java8 新特性学习 Lambda表达式 和 Stream 用法案例
Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...
- jdk1.8新特性之lambda表达式及在Android Studio中的使用举例
Jdk1.8已经出很久了但是很多同学对它的特性在android studio 中的应用可能还不是很熟悉,今天我们就来对这个新特性在AS中做它的应用实践. 一.首先在有JDK1.8的情况下我们要在AS的 ...
- Java 8新特性之lambda(八恶人-2)
Major Marquis Warren 沃伦·马奎斯少校 “Tring to get a couple of bounties in to Red Rock.”我想带几个通缉犯去红石镇 一.基本介绍 ...
- Java8新特性之Lambda表达式
lambda表达式是java8给我们带来的几个重量级新特性之一,借用lambda表达式,可以让我们的java程序设计更加简洁.最近新的项目摒弃了1.6的版本,全面基于java8进行开发,本文是java ...
- C++11新特性之一——Lambda表达式
C++11新特性总结可以参考:http://www.cnblogs.com/pzhfei/archive/2013/03/02/CPP_new_feature.html#section_6.8 C++ ...
随机推荐
- Python(算法)-时间复杂度和空间复杂度
时间复杂度 算法的时间复杂度是一个函数,它定量描述了该算法的运行时间,时间复杂度常用“O”表述,使用这种方式时,时间复杂度可被称为是渐近的,它考察当输入值大小趋近无穷时的情况 时间复杂度是用来估计算法 ...
- springboot2.0动态多数据源切换
摘要:springboot1.x到springboot2.0配置变化有一点变化,网上关于springboot2.0配置多数据源的资料也比较少,为了让大家配置多数据源从springboot1.x升级到s ...
- PyQt--QTreeWidget
转载:loster_Li QTreeWidget的继承关系如下图: 因为继承关系是 QAbstractItemView->QTreeView->QTreeWidget ,所以和QTabl ...
- leetcode739
class Solution(object): def dailyTemperatures(self, T: 'List[int]') -> 'List[int]': S = list() n ...
- ElasicSearch(2) Linux运行
1.org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearc ...
- ajax用FormData方式提交
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jsonArray返回
dao <select id="selectShopInfo" resultType="java.util.HashMap"> SELECT * F ...
- actuator/hystrix.stream 没有反应的方法
http://localhost:8086/actuator/hystrix.stream 在启动类加上,就ok了 @Bean public ServletRegistrationBean hystr ...
- Django_models下划线__正反查询,对象正反查询
1.我们使用models对数据库进行查询的时候,想去拿到结果的时候有时直接使用".字段",有时是'[0].字段',区别就是我们使用的语句返回的到底是一个对象还是列表: obj=mo ...
- 好看的alert弹出框sweetalert
转载:https://www.cnblogs.com/lamp01/p/7215408.html